#include <bits/stdc++.h> n using namespace std; n int n, m, sz; n int par[4000005]; n int findpar(int x) { n   if (par[x] == x) return par[x]; n   par[x] = findpar(par[x]); n   return par[x]; n } n int cr, cc; n int pr, pc; n void decode(int x) { n   if (x < m) { n     cr = 0; n     cc = x; n   } else if (x < m + n - 1) { n     cr = x - m + 1; n     cc = m - 1; n   } else if (x < 2 * m + n - 2) { n     cr = n - 1; n     cc = m - 1 - (x - (m + n - 2)); n   } else { n     cr = n - 1 - (x - (2 * m + n - 3)); n     cc = 0; n   } n } n int encode(int r, int c) { n   if (r == 0) return c; n   if (c == m - 1) return m - 1 + r; n   if (r == n - 1) return m + n - 2 + m - 1 - c; n   if (c == 0) return 2 * m + n - 3 + n - 1 - r; n } n bool inrange(int x, int y) { return 0 <= x && x < n && 0 <= y && y < m; } n void go(int r, int c, int dr, int dc) { n   int x; n   if (dr == -1 && dc == -1) x = min(r, c); n   if (dr == -1 && dc == 1) x = min(r, m - 1 - c); n   if (dr == 1 && dc == -1) x = min(n - 1 - r, c); n   if (dr == 1 && dc == 1) x = min(n - 1 - r, m - 1 - c); n   pr = r + x * dr; n   pc = c + x * dc; n } n int main() { n   cin >> n >> m; n   sz = 2 * (n + m) - 4; n   for (int i = 0; i < sz; ++i) par[i] = i; n   for (int i = 0; i < sz; ++i) { n     decode(i); n     for (int dr = -1; dr <= 1; dr += 2) n       for (int dc = -1; dc <= 1; dc += 2) { n         if (inrange(cr + dr, cc + dc)) { n           go(cr, cc, dr, dc); n           int x = encode(pr, pc); n           int px = findpar(x); n           int pi = findpar(i); n           if (px != pi) { n             par[max(pi, px)] = min(pi, px); n           } n         } n       } n   } n   set<int> st; n   for (int i = 0; i < sz; ++i) st.insert(findpar(i)); n   cout << st.size() << endl; 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 r; n   cin >> r; n   if ((r - 1) % 2 == 0 && r > 3) n     cout << 1 <<     << (r - 3) / 2; n   else n     cout <<  NO ; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n int const nmax = 3000; n int const modulo = 1000000007; n char v[1 + nmax][1 + nmax]; n int dp[1 + nmax][1 + nmax]; n int solve(int x, int y, int x2, int y2) { n   for (int i = 1; i <= x2; i++) n     for (int j = 1; j <= y2; j++) dp[i][j] = 0; n   dp[x][y] = 1; n   for (int i = 1; i <= x2; i++) n     for (int j = 1; j <= y2; j++) { n       dp[i][j] += dp[i - 1][j] + dp[i][j - 1]; n       if (modulo <= dp[i][j]) dp[i][j] -= modulo; n       if (v[i][j] ==  # ) dp[i][j] = 0; n     } n   return dp[x2][y2]; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n, m; n   cin >> n >> m; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) cin >> v[i][j]; n   int result = 1LL * solve(1, 2, n - 1, m) * solve(2, 1, n, m - 1) % modulo; n   int result2 = 1LL * solve(1, 2, n, m - 1) * solve(2, 1, n - 1, m) % modulo; n   cout << (modulo + result - result2) % modulo; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int fastpow(int a, int b) { n   int ans = 1; n   for (; b; b >>= 1, a = a * a) n     if (b & 1) ans = ans * a; n   return ans; n } n int num; n int main() { n   int n; n   scanf( %d , &n); n   string a; n   cin >> a; n   for (int i = 0; i < a.length(); i++) n += (a[i] -  0 ) * fastpow(10, i); n   printf( %d , n); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   scanf( %d , &n); n   for (int z = 0; z < n; z++) { n     long long a, b, c; n     cin >> a >> b; n     while ((c = a | a + 1) <= b) a = (a | a + 1); n     cout << a << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O2 ) n using namespace std; n const double PI = atan(1.0) * 4; n const int di[4] = {-1, 0, 1, 0}; n const int dj[4] = {0, -1, 0, 1}; n const long long INF = (long long)2e18 + 50; n const int maximum = numeric_limits<int>::min(); n const int minimum = numeric_limits<int>::max(); n constexpr int N = 2e6, P = 1000000007; n int a[N + 1], b[N + 1]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   for (int i = 1; i <= N; ++i) { n     a[i] = (2ll * a[i - 2] + a[i - 1]) % P; n     if (b[i - 1] && b[i - 2]) { n       a[i] = (a[i] + 1) % P; n     } else { n       b[i] = 1; n     } n   } n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     cout << 4ll * a[n] % P <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n const int N = 5050; n int a[N]; n int dp[N][N]; n int cnt[N]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cerr.setstate(ios_base::failbit); n   cin >> n >> k; n   for (int i = 1; i < (n + 1); i++) cin >> a[i]; n   sort(a + 1, a + n + 1); n   int j = n; n   for (int i = (n + 1) - 1; i >= 1; i--) { n     while (j > 0 && abs(a[j] - a[i]) <= 5) j--; n     cnt[i] = i - j; n   } n   for (int i = 1; i < (n + 1); i++) n     for (int j = 1; j < (k + 1); j++) { n       int &ans = dp[i][j]; n       ans = max(ans, dp[i - 1][j]); n       ans = max(ans, dp[i - cnt[i]][j - 1] + cnt[i]); n     } n   cout << dp[n][k] <<   n ; n } n


#include  bits/stdc++.h  n using namespace std; n #define ll long long n #define llu unsigned long long n #define PB push_back n #define F first n #define S second n #define sz(a) ((ll)a.size()) n const ll MOD = 1000000007; n const ll INF = 1000000000000000007; n #ifdef ONLINE_JUDGE n #define ck(x...) n #endif n  n void AC_aaega(){ n  ll n; n  cin>>n; n  vector < vector < ll > > vv(n, vector < ll >(n, 0)); n  vector < ll > k1(10, INF); n  vector < ll > k2(10, -1); n  vector < ll > q1(10, INF); n  vector < ll > q2(10, -1); n  for(ll i =0;i<n;i++){ n   string s; n   cin>>s; n   for(ll j= 0;j<n;j++){ n    vv[i][j] = s[j] -  0 ; n    k1[vv[i][j]] = min(k1[vv[i][j]], j); n    k2[vv[i][j]] = max(k2[vv[i][j]], j); n    q1[vv[i][j]] = min(q1[vv[i][j]], i); n    q2[vv[i][j]] = max(q2[vv[i][j]], i); n   } n  }  n  ck(k1, k2, q1, q2); n  for(ll i= 0;i<10;i++){ n   ll ans =0, ff =-1, ss = -1; n   for(ll j =0;j<n;j++){ n    for(ll k =0;k<n;k++){ n     if(vv[j][k] == i){ n      if(ff == -1){ n       ff = k; n      } n      else{ n       ss = k; n      } n     } n    } n    if(ss != -1 && ff != -1){ n     ans = max(ans, (ss - ff) * max(j, n - 1 - j)); n     ck(j, ss, ff); n    } n    if(ff != -1){ n     ll qw = q1[i], wq = q2[i]; n     if(qw != INF && wq != -1){ n      ans = max(ans, max(abs(n-1-ff), ff) * max(j - qw, wq - j)); n     } n     // else if(qw != -1){ n     //  ans = max(ans, max(abs(n-1-ff), ff) * j-qw); n     // } n    } n    if(ss != -1){ n     ll qw = q1[i], wq = q2[i]; n     if(qw != INF && wq != -1){ n      ans = max(ans, max(abs(n-1-ss), ss) * max(j - qw, wq - j)); n     } n     // else if(qw != -1){ n     //  ans = max(ans, max(abs(n-1-ff), ff) * j-qw); n     // } n    } n    ff = ss = -1; n   } n   ck(ans); n   for(ll j =0;j<n;j++){ n    for(ll k =0;k<n;k++){ n     if(vv[k][j] == i){ n      if(ff == -1){ n       ff = k; n      } n      else{ n       ss = k; n      } n     } n    } n    if(ss != -1 && ff != -1){ n     ans = max(ans, (ss - ff) * max(j, n - 1 - j)); n    } n    if(ff != -1){ n     ll qw = k1[i], wq = k2[i]; n     if(qw != INF && wq != -1){ n      ans = max(ans, max(abs(n-1-ff), ff) * max(j - qw, wq - j)); n     } n     ck(ff, qw, wq); n     // else if(qw != -1){ n     //  ans = max(ans, max(abs(n-1-ff), ff) * j-qw); n     // } n    } n    if(ss != -1){ n     ll qw = k1[i], wq = k2[i]; n     if(qw != INF && wq != -1){ n      ans = max(ans, max(abs(n-1-ss), ss) * max(j - qw, wq - j)); n     } n     ck(ss, qw, wq); n     // else if(qw != -1){ n     //  ans = max(ans, max(abs(n-1-ff), ff) * j-qw); n     // } n    } n    ff = ss = -1; n   } n   cout<<ans<<   ; n  } n  cout<<  n ; n } n  n int main(){ n  ios_base::sync_with_stdio(0); n  cin.tie(0); n  cout.tie(0); n  cerr.tie(0); n  #ifndef ONLINE_JUDGE n    freopen( /home/theviralv/Desktop/input.txt ,  r , stdin); n    freopen( /home/theviralv/Desktop/output.txt ,  w , stdout); n    freopen( /home/theviralv/Desktop/err.txt ,  w , stderr); n  #endif n  ll testcases = 1; n  cin>>testcases; n  while(testcases--){ n   AC_aaega(); n  }    n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n const double eps = 0.001; n const int mod = 1000000007; n int main() { n   int n, pos, l, r, ans; n   scanf( %d%d%d%d , &n, &pos, &l, &r); n   if (l == 1 && r == n) n     ans = 0; n   else if (l == 1) n     ans = abs(pos - r) + 1; n   else if (r == n) n     ans = abs(pos - l) + 1; n   else n     ans = min(abs(pos - l), abs(pos - r)) + r - l + 2; n   printf( %d , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int q, s1, s2, s3, w, m, e; n   vector<long long int> v; n   cin >> q; n   long long int n; n   for (int i = 0; i < q; i++) { n     cin >> n; n     long long int a[n]; n     s1 = 0; n     s2 = 0; n     s3 = 0; n     for (int j = 0; j < n; j++) { n       cin >> a[j]; n       if (a[j] % 3 == 0) { n         s3++; n       } else if (a[j] % 3 == 1) { n         s1++; n       } else if (a[j] % 3 == 2) { n         s2++; n       } n     } n     w = (s3 + min(s1, s2)); n     m = s1; n     e = s2; n     m -= min(s1, s2); n     e -= min(s1, s2); n     s1 = m; n     s2 = e; n     w += s1 / 3; n     w += s2 / 3; n     v.push_back(w); n   } n   for (int i = 0; i < q; i++) { n     cout << v[i] << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 5e5 + 10; n const int inf = 1e9; n struct node { n   int s, ind; n } f[maxn]; n int sum[maxn]; n int cmp(node a, node b) { n   if (a.s == b.s) return a.ind < b.ind; n   return a.s < b.s; n } n int main() { n   int i, n, c, re = 0, value; n   scanf( %d%d , &n, &c); n   for (i = 1; i <= n; i++) { n     scanf( %d , &f[i].s); n     f[i].ind = i; n     sum[i] = sum[i - 1] + (f[i].s == c); n   } n   sort(f + 1, f + n + 1, cmp); n   for (i = 1; i <= n; i++) { n     if (f[i].s == c) continue; n     if (f[i].s != f[i - 1].s) { n       value = 1; n       re = max(re, value); n       continue; n     } n     value -= (sum[f[i].ind - 1] - sum[f[i - 1].ind]); n     if (value < 0) value = 0; n     value++; n     re = max(re, value); n   } n   printf( %d , re + sum[n]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void print_err() { cerr <<   n ; } n template <class T, class... Arg> n void print_err(T x, Arg &&...args) { n   cerr << x <<    ; n   print_err(args...); n } n template <class T> n void print_container(T &cont) { n   for (auto iter : cont) { n     cerr << iter <<    ; n   } n   cerr <<   n ; n } n int mod = 998244353; n long long ceil(long long a, long long b) { return (a + b - 1) / b; } n long long gcd(long long a, long long b) { n   if (b == 0) { n     return a; n   } n   return gcd(b, a % b); n } n vector<int> d4x{0, 0, 1, -1}; n vector<int> d4y{-1, 1, 0, 0}; n vector<int> d8x{0, 0, 1, -1, 1, 1, -1, -1}; n vector<int> d8y{1, -1, 0, 0, -1, 1, 1, -1}; n vector<long long> B(1e5 + 1, -1), C(1e5 + 1, -1); n void solve(int test) { n   ; n   int n; n   cin >> n; n   vector<int> A(n); n   for (int &a : A) { n     cin >> a; n   } n   long long ans = 0; n   vector<long long> dp1, dp2; n   int j; n   long long val; n   for (int i = n - 1; i >= 0; --i) { n     for (auto iter : dp1) { n       j = iter; n       val = B[j]; n       if (val < 0) { n         ; n       } n       if (val > 0) { n         if (j >= A[i]) { n           if (C[A[i]] == -1) { n             dp2.push_back(A[i]); n             C[A[i]] = val; n             C[A[i]] %= mod; n           } else { n             C[A[i]] += val; n             C[A[i]] %= mod; n           } n         } else { n           if (C[A[i] / ceil(A[i], j)] == -1) { n             dp2.push_back(A[i] / ceil(A[i], j)); n             C[A[i] / ceil(A[i], j)] = val; n             ans += (i + 1) * (ceil(A[i], j) - 1) * val; n             ans %= mod; n             C[A[i] / ceil(A[i], j)] %= mod; n           } else { n             C[A[i] / ceil(A[i], j)] += val; n             ans += (i + 1) * (ceil(A[i], j) - 1) * val; n             ans %= mod; n             C[A[i] / ceil(A[i], j)] %= mod; n           } n         } n       } n       B[j] = -1; n     } n     dp1.clear(); n     swap(dp1, dp2); n     swap(B, C); n     if (B[A[i]] == -1) { n       dp1.push_back(A[i]); n       B[A[i]] = 1; n     } else { n       B[A[i]]++; n       B[A[i]] %= mod; n     } n   } n   for (int j : dp1) { n     B[j] = -1; n   } n   cout << ans << endl; n } n int main() { n   int i = 1; n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int test_cases; n   cin >> test_cases; n   for (i = 1; i <= test_cases; ++i) solve(i); 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     bool impr = false; n     bool taken[n + 1]; n     for (int i = 0; i <= n; i++) { n       taken[i] = false; n     } n     int notTaken = -1; n     for (int i = 0; i < n; i++) { n       int k; n       cin >> k; n       bool done = false; n       for (int i = 0; i < k; i++) { n         int next; n         cin >> next; n         if (!done && !taken[next]) { n           taken[next] = true; n           done = true; n         } n       } n       if (!done) { n         impr = true; n         notTaken = i + 1; n       } n     } n     if (impr) { n       cout <<  IMPROVE  << endl; n       int good = 0; n       for (int i = 1; i <= n; i++) { n         if (taken[i] == false) { n           good = i; n         } n       } n       cout << notTaken <<     << good << endl; n     } else { n       cout <<  OPTIMAL  << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long int N, T; n void solve() { n   cin >> N; n   vector<long long int> cnt(N + 1); n   long long int x; n   for (long long int i = 0; i < N; i++) { n     cin >> x; n     cnt[x]++; n   } n   long long int ans = 0; n   for (long long int s = 2; s <= 2 * N; s++) { n     long long int cur = 0; n     for (long long int i = 1; i < (s + 1) / 2; i++) { n       if (s - i > N) n         continue; n       else { n         cur += min(cnt[i], cnt[s - i]); n       } n     } n     if (s % 2 == 0) { n       cur += cnt[s / 2] / 2; n     } n     ans = max(ans, cur); n   } n   cout << ans <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> T; n   while (T--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char s[200001], out[20]; n int len[200001]; n int ans1[2000001], ans2[2000001]; n vector<int> v[2000001]; n int digit[6]; n bool solve(int n, int i, int sum) { n   if (!i) { n     if (ans1[sum] >= 10) return false; n     ans2[n] = sum; n     return true; n   } n   for (int j = 1, k = 10; k <= i * 10; j++, k *= 10) { n     if (solve(n, i / k, sum + i % k)) { n       v[n].push_back(j); n       return true; n     } n   } n   return false; n } n int main() { n   int n; n   for (int i = 1; i <= 2000000; i++) { n     int j = 0; n     for (; digit[j] == 9; digit[j++] = 0) n       ; n     digit[j]++; n     int sum = 0; n     for (int j = 0; j < 6; j++) sum += digit[j]; n     ans1[i] = sum; n   } n   for (int i = 1; i <= 2000000; i++) { n     v[i].reserve(7); n     solve(i, i, 0); n   } n   scanf( %d%s , &n, s); n   int sum; n   while (1) { n     if (n > 10) { n       int t = rand() % min(n / 2, 500); n       for (int i = 0; i < t; i++) len[i] = 2; n       for (int i = 2 * t; i < n; i++) len[i - t] = 1; n       random_shuffle(len, len + n - t); n     } else n       for (int i = 0; i < n; i++) len[i] = 1; n     sum = 0; n     for (int i = 0, j = 0; j < n; j += len[i++]) n       sum += len[i] == 1 ? s[j] -  0  : (s[j] -  0 ) * 10 + s[j + 1] -  0 ; n     if (!v[sum].empty()) break; n   } n   for (int i = 0, j = 0; j < n; j += len[i++]) { n     if (i) putchar( + ); n     putchar(s[j]); n     if (len[i] > 1) putchar(s[j + 1]); n   } n   putchar(  n ); n   sprintf(out,  %d , sum); n   int j = 0; n   for (int i : v[sum]) { n     if (j) putchar( + ); n     for (int k = 0; k < i; k++) putchar(out[j + k]); n     j += i; n   } n   putchar(  n ); n   sum = ans2[sum]; n   sprintf(out,  %d , sum); n   for (int i = 0; out[i]; i++) { n     if (i) putchar( + ); n     putchar(out[i]); n   } n   putchar(  n ); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long a, b, c; n   cin >> a >> b >> c; n   long long Min = min(a, b); n   Min = min(Min, c); n   long long day = 0; n   while (a + b + c >= 7 && a >= 3 && b >= 2 && c >= 2) { n     a -= 3; n     b -= 2; n     c -= 2; n     day += 7; n   } n   long long ans = 0; n   long long _a = a, _b = b, _c = c; n   for (long long i = 1; i <= 7; i++) { n     long long tmp = 0; n     a = _a; n     b = _b; n     c = _c; n     for (long long j = i; j <= i + 7; j++) { n       long long _j = j % 7; n       if (_j == 1 || _j == 4 || _j == 0) { n         if (!a) { n           ans = max(ans, tmp); n           break; n         } n         a--; n         tmp++; n       } n       if (_j == 2 || _j == 6) { n         if (!b) { n           ans = max(ans, tmp); n           break; n         } n         b--; n         tmp++; n       } n       if (_j == 3 || _j == 5) { n         if (!c) { n           ans = max(ans, tmp); n           break; n         } n         c--; n         tmp++; n       } n     } n     ans = max(ans, tmp); n   } n   cout << day + ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename Arg1> n void ZZ(const char* name, Arg1&& arg1) { n   std::cerr << name <<   =   << arg1 << endl; n } n template <typename Arg1, typename... Args> n void ZZ(const char* names, Arg1&& arg1, Args&&... args) { n   const char* comma = strchr(names + 1,  , ); n   std::cerr.write(names, comma - names) <<   =   << arg1; n   ZZ(comma, args...); n } n int n, u, v; n int nxt[1000010], ans[1000010], cnt[1000010]; n pair<int, int> a[1000000]; n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   ; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i].first >> a[i].second; n     cnt[a[i].second] = 1; n     nxt[a[i].first] = a[i].second; n     if (!a[i].first) u = a[i].second; n   } n   for (int i = 0; i < n; i++) { n     if (!cnt[a[i].first]) { n       v = a[i].first; n       break; n     } n   } n   for (int i = 0; i < n; i += 2) { n     ans[i] = v; n     v = nxt[v]; n   } n   for (int i = 1; i < n; i += 2) { n     ans[i] = u; n     u = nxt[u]; n   } n   for (int i = 0; i < n; i++) cout << ans[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(2) n #pragma GCC optimize(3) n using namespace std; n int a[200011], b[200011]; n struct song { n   int t; n   int id; n   bool operator<(song other) const { n     if (t != other.t) return t < other.t; n     return id < other.id; n   } n }; n struct oriSet { n   int tol = 0; n   int len = 0; n   set<song> s; n   bool empty() { return s.empty(); } n   int size() { return s.size(); } n   void insert(int id) { n     s.insert(song{a[id], id}); n     tol += b[id]; n     len += a[id]; n   } n   int extractmin() { n     if (!s.empty()) { n       return s.begin()->id; n     } n     return -1; n   } n   int extractmax() { n     if (!s.empty()) { n       return s.rbegin()->id; n     } n     return -1; n   } n   void del(int id) { n     if (s.find(song{a[id], id}) != s.end()) { n       s.erase(song{a[id], id}); n       tol -= b[id]; n       len -= a[id]; n     } n   } n }; n struct lessSet { n   int tol = 0; n   int len = 0; n   set<song> s; n   bool empty() { return s.empty(); } n   int size() { return s.size(); } n   void insert(int id) { n     int ti = a[id] - a[id] / 2; n     s.insert(song{a[id], id}); n     tol += b[id]; n     len += ti; n   } n   int extractmin() { n     if (!s.empty()) { n       return s.begin()->id; n     } n     return -1; n   } n   int extractmax() { n     if (!s.empty()) { n       return s.rbegin()->id; n     } n     return -1; n   } n   void del(int id) { n     if (s.find(song{a[id], id}) != s.end()) { n       s.erase(song{a[id], id}); n       tol -= b[id]; n       len -= a[id] - a[id] / 2; n     } n   } n }; n oriSet s1; n lessSet s2; n int n, w, k; n void make_balance() { n   while (s2.size() < w && !s1.empty()) { n     int id = s1.extractmax(); n     s1.del(id); n     s2.insert(id); n   } n   while (!s1.empty() && s2.size() == w) { n     int id1 = s1.extractmax(), id2 = s2.extractmin(); n     if (a[id1] <= a[id2]) break; n     if (a[id1] > a[id2]) { n       s1.del(id1); n       s2.del(id2); n       s1.insert(id2); n       s2.insert(id1); n     } n   } n } n int main() { n   scanf( %d%d%d , &n, &w, &k); n   int i, j, ans = 0; n   for (i = 1; i <= n; i++) { n     scanf( %d , &b[i]); n   } n   for (i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n   } n   for (i = 1, j = 1; i <= n; i++) { n     s1.insert(i); n     make_balance(); n     while (j <= i && s1.len + s2.len > k) { n       s1.del(j); n       s2.del(j); n       make_balance(); n       j++; n     } n     ans = max(ans, s1.tol + s2.tol); n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long n, k, ar[200000]; n long long reach[777777]; n long long q; n long long ans[777777]; n multiset<long long> bst; n multiset<long long>::iterator it; n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n >> k; n   for (int i = 1; i <= n; i++) cin >> ar[i]; n   reach[0] = 1; n   for (int i = 1; i <= 666666; i++) reach[i] = 0; n   for (int i = 1; i <= n; i++) n     for (int j = 555555; j + 1; --j) n       if (reach[j]) reach[j + ar[i]] = 1; n   ans[0] = 0; n   bst.insert(0); n   for (int i = 1; i <= 666666; i++) { n     ans[i] = 1e18; n     if (i > k && ans[i - k - 1] < 1e18) { n       it = bst.find(ans[i - k - 1]); n       bst.erase(it); n     } n     if (reach[i] == 0) continue; n     if (bst.size() == 0) continue; n     it = bst.begin(); n     q = (*it); n     ans[i] = q + 1; n     bst.insert(ans[i]); n   } n   q = 666666; n   while (ans[q] > 1e17) --q; n   cout << q <<     << ans[q] << endl; n   cin.get(); n   cin.get(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N, C; n int A[1001]; n vector<int> V; n int main() { n   cin >> N; n   for (int i = 0; i < N; ++i) cin >> A[i]; n   for (int i = 0; i < N; ++i) { n     C = 1; n     for (int j = i; j < N - 1; ++j) { n       if (A[j] >= A[j + 1]) n         ++C; n       else n         break; n     } n     for (int j = i; j > 0; --j) { n       if (A[j] >= A[j - 1]) n         ++C; n       else n         break; n     } n     V.push_back(C); n   } n   sort(V.begin(), V.end()); n   cout << V[V.size() - 1] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int nsz = 5e5; n int n, rt = 1, t, l[nsz + 5], r[nsz + 5]; n vector<int> g[nsz + 5]; n void dfs(int u = rt, int p = rt) { n   for (int i = 0; i < g[u].size(); ++i) { n     int v = g[u][i]; n     if (v == p) continue; n     l[v] = ++t; n   } n   r[u] = ++t; n   for (int i = g[u].size() - 1; i >= 0; --i) { n     int v = g[u][i]; n     if (v == p) continue; n     dfs(v, u); n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n - 1; ++i) { n     int u, v; n     scanf( %d%d , &u, &v); n     g[u].push_back(v); n     g[v].push_back(u); n   } n   l[rt] = ++t; n   dfs(); n   for (int u = 1; u <= n; ++u) { n     cout << l[u] <<     << r[u] <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n inline long long Qmul(const long long &x, const long long &y, n                       const long long &X) { n   long long k = (long long)((1.0L * x * y) / (1.0L * X)), t = x * y - k * X; n   t -= X; n   while (t < 0) t += X; n   return t; n } n class MillerRabin { n  private: n   const int P[12] = {2, 3, 5, 7, 11, 13, 17, 19, 61, 2333, 4567, 24251}; n   inline long long Qpow(long long x, long long y, long long X) { n     long long t = 1; n     while (y) y & 1 && (t = Qmul(t, x, X)), x = Qmul(x, x, X), y >>= 1; n     return t; n   } n   inline bool Check(const long long &x, const int &p) { n     if (!(x % p) || Qpow(p % x, x - 1, x) ^ 1) return false; n     long long k = x - 1, t; n     while (!(k & 1)) { n       if ((t = Qpow(p % x, k >>= 1, x)) ^ 1 && t ^ (x - 1)) return false; n       if (!(t ^ (x - 1))) return true; n     } n     return true; n   } n  n  public: n   bool IsPrime(const long long &x) { n     if (x < 2) return false; n     for (int i = 0; i ^ 12; ++i) { n       if (!(x ^ P[i])) return true; n       if (!Check(x, P[i])) return false; n     } n     return true; n   } n }; n class PollardRho { n  private: n   long long ans; n   MillerRabin MR; n   inline long long gcd(const long long &x, const long long &y) { n     return y ? gcd(y, x % y) : x; n   } n   inline long long Work(const long long &x, const int &y) { n     int t = 0, k = 1; n     long long v0 = (1LL * rand() * rand() * rand() * rand() % (x - 1) + 1), n               v = v0, d, s = 1; n     while (1) { n       if (v = (Qmul(v, v, x) + y) % x, n           s = Qmul(s, ((v - v0) < 0 ? -(v - v0) : (v - v0)), x), n           !(v ^ v0) || !s) n         return x; n       if (++t == k) { n         if ((d = gcd(s, x)) ^ 1) return d; n         v0 = v, k <<= 1; n       } n     } n   } n   inline void Resolve(long long x, int t) { n     if (!(x ^ 1) || x <= ans) return; n     if (MR.IsPrime(x)) return (void)(ans < (x) && (ans = (x))); n     long long y = x; n     while ((y = Work(x, t--)) == x) n       ; n     while (!(x % y)) x /= y; n     Resolve(x, t), Resolve(y, t); n   } n  n  public: n   inline PollardRho() { srand(20050521); } n   inline long long GetMax(const long long &x) { n     return ans = 0, Resolve(x, 302627441), ans; n   } n } P; n int T, p, ans[10010]; n long long pr[60], dis[100010], head[100010], o; n struct edge { n   int to, link; n   long long w; n } e[6000010]; n struct node { n   int id; n   long long w; n   bool operator<(const node &b) const { return w > b.w; } n }; n priority_queue<node> Q; n struct ask { n   long long n, k, id; n   bool operator<(const ask &b) const { return k < b.k; } n } q[10010]; n void add_edge(int u, int v, long long w) { n   e[++o].to = v, e[o].link = head[u], head[u] = o, e[o].w = w; n } n long long exgcd(long long a, long long b, long long &x, long long &y) { n   if (!b) return x = 1, y = 0, a; n   long long t = exgcd(b, a % b, y, x); n   return y -= a / b * x, t; n } n int main() { n   scanf( %d , &T); n   for (int i = 1; i <= T; i++) scanf( %lld%lld , &q[i].n, &q[i].k), q[i].id = i; n   sort(q + 1, q + T + 1); n   for (int l = 1, r = 0; l <= T; l = r + 1) { n     while (r < T && q[r + 1].k == q[l].k) r++; n     long long k = q[l].k; n     if (k == 1) continue; n     p = 0; n     while (k > 1) { n       pr[++p] = P.GetMax(k); n       while (k % pr[p] == 0) k /= pr[p]; n     } n     if (p == 1) { n       for (int i = l; i <= r; i++) ans[q[i].id] = (q[i].n % pr[1] == 0); n       continue; n     } n     if (p == 2) { n       long long x, y; n       exgcd(pr[1], pr[2], x, y); n       x = (x % pr[2] + pr[2]) % pr[2]; n       for (int i = l; i <= r; i++) { n         long long b = q[i].n - Qmul(x, q[i].n, pr[2]) * pr[1]; n         ans[q[i].id] = (b >= 0); n       } n       continue; n     } n     int L = pr[p]; n     o = 0; n     for (int i = 1; i < L; i++) dis[i] = LLONG_MAX; n     for (int i = 0; i < L; i++) { n       head[i] = 0; n       for (int j = 1; j < p; j++) add_edge(i, (i + pr[j]) % L, pr[j]); n     } n     Q.push((node){0, dis[0] = 0}); n     while (Q.size()) { n       int u = Q.top().id; n       Q.pop(); n       for (int i = head[u]; i; i = e[i].link) n         if (e[i].w + dis[u] < dis[e[i].to]) { n           dis[e[i].to] = dis[u] + e[i].w; n           Q.push((node){e[i].to, dis[e[i].to]}); n         } n     } n     for (int i = l; i <= r; i++) ans[q[i].id] = (q[i].n >= dis[q[i].n % L]); n   } n   for (int i = 1; i <= T; i++) puts(ans[i] ?  YES  :  NO ); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = int(5e5) + 3; n const int MOD = 998244353; n const double PI = 4 * atan(1); n const double eps = 1e-10; n const long long oo = 1e10; n const int K = 26; n int n, m; n int a[N], b[N], c[N], d[N], k[N]; n vector<int> actors[N]; n vector<int> pitch[N]; n set<int> s; n unordered_map<int, int> maa; n unordered_map<int, set<int> > occ; n vector<int> songs[N]; n int anss[N]; n void prepAns() { n   for (int i = 0; i < m; i++) { n     for (auto j : songs[i]) { n       anss[j] = i; n     } n   } n } n bool exists(int c, int j) { n   set<int>::iterator it; n   bool cond = false; n   if (s.count(c - 1) == 0) { n     cond = true; n     s.insert(c - 1); n   } n   it = s.find(c - 1); n   it++; n   if (it == s.end()) { n     if (cond) { n       s.erase(c - 1); n     } n     return 0; n   } n   int x = *it; n   int y = *occ[x].begin(); n   songs[j].push_back(y); n   occ[x].erase(occ[x].begin()); n   if (occ[x].size() == 0) { n     s.erase(x); n   } n   if (cond) { n     s.erase(c - 1); n   } n   return 1; n } n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i] >> b[i]; n     s.insert(a[i]); n     s.insert(b[i]); n   } n   cin >> m; n   s.insert(0); n   for (int i = 0; i < m; i++) { n     cin >> c[i] >> d[i] >> k[i]; n     s.insert(c[i]); n     s.insert(d[i]); n   } n   int ind = 1; n   for (auto t : s) { n     maa[t] = ind++; n   } n   for (int i = 0; i < n; i++) { n     a[i] = maa[a[i]]; n     b[i] = maa[b[i]]; n     pitch[b[i]].push_back(i); n   } n   for (int i = 0; i < m; i++) { n     c[i] = maa[c[i]]; n     d[i] = maa[d[i]]; n     actors[d[i]].push_back(i); n   } n   maa.clear(); n   s.clear(); n   int ans = 0; n   for (int i = 1; i < ind; i++) { n     for (auto j : pitch[i]) { n       s.insert(a[j]); n       occ[a[j]].insert(j); n     } n     for (auto j : actors[i]) { n       while (k[j] && exists(c[j], j)) { n         k[j]--; n         ans++; n       } n     } n   } n   if (ans == n) { n     cout <<  YES n ; n     prepAns(); n     for (int i = 0; i < n; i++) { n       cout << anss[i] + 1 <<    ; n     } n   } else { n     cout <<  NO n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int TOUR = 1 << 18, INF = 0x3f3f3f3f; n struct strq { n   int r1, r2, s1, s2, ind; n   strq() {} n } q[TOUR]; n struct event { n   int st, red, stup; n   strq qq; n   event(int st, int red, int stup, strq qq) n       : st(st), red(red), stup(stup), qq(qq) {} n   event() {} n }; n bool operator<(event a, event b) { n   if (a.red != b.red) return a.red < b.red; n   return a.st > b.st; n } n int t[2 * TOUR]; n int red[TOUR], stup[TOUR]; n int brred, brstup; n int rje[TOUR]; n int indexxR[TOUR]; n int n, brq; n vector<event> V; n vector<int> R[TOUR]; n void Stavi(int pos, int val) { n   for (t[pos + TOUR] = val, pos = (pos + TOUR) / 2; pos; pos /= 2) n     t[pos] = max(t[2 * pos], t[2 * pos + 1]); n } n int Vrati(int pos, int lo, int hi, int begin, int end) { n   if (lo >= end || hi <= begin) return 0; n   if (lo >= begin && hi <= end) return t[pos]; n   return max(Vrati(2 * pos + 0, lo, (lo + hi) / 2, begin, end), n              Vrati(2 * pos + 1, (lo + hi) / 2, hi, begin, end)); n } n void Promjena() { n   for (int i = 0; i < n; i++) swap(red[i], stup[i]); n   for (int i = 0; i < brq; i++) { n     swap(q[i].r1, q[i].s1); n     swap(q[i].r2, q[i].s2); n   } n } n void Init() { n   V.clear(); n   for (int i = 0; i < TOUR; i++) R[i].clear(); n   for (int i = 0; i < n; i++) { n     V.push_back(event(0, red[i], stup[i], strq())); n     R[stup[i]].push_back(red[i]); n   } n   for (int i = 0; i < brq; i++) V.push_back(event(1, q[i].r1, q[i].s1, q[i])); n   memset(t, INF, sizeof t); n   memset(indexxR, 0, sizeof indexxR); n   for (int i = 0; i < TOUR; i++) { n     R[i].push_back(INF); n     sort(R[i].begin(), R[i].end()); n     Stavi(i, R[i][0]); n   } n   sort(V.begin(), V.end()); n } n int main() { n   scanf( %d%d%d%d , &brred, &brstup, &n, &brq); n   for (int i = 0; i < n; i++) scanf( %d%d , &red[i], &stup[i]); n   for (int i = 0; i < brq; i++) { n     scanf( %d%d%d%d , &q[i].r1, &q[i].s1, &q[i].r2, &q[i].s2); n     q[i].ind = i; n   } n   for (int i = 0; i < 2; i++) { n     Init(); n     for (int j = 0; j < (int)V.size(); j++) { n       event tmp = V[j]; n       if (!tmp.st) { n         indexxR[tmp.stup]++; n         Stavi(tmp.stup, R[tmp.stup][indexxR[tmp.stup]]); n       } else { n         if (Vrati(1, 0, TOUR, tmp.qq.s1, tmp.qq.s2 + 1) <= tmp.qq.r2) n           rje[tmp.qq.ind] = 1; n       } n     } n     Promjena(); n   } n   for (int i = 0; i < brq; i++) { n     if (rje[i]) n       printf( YES n ); n     else n       printf( NO n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int nextt[200007], c[200007], vis[200007]; n long long ans, n; n template <class T> n void read(T &x) { n   int f = 0; n   x = 0; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     f |= (ch ==  - ); n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = (x << 1) + (x << 3) + (ch ^ 48); n     ch = getchar(); n   } n   x = f ? -x : x; n } n int MIN(int now) { n   int min = c[now]; n   for (int i = nextt[now];; i = nextt[i]) { n     if (i == now) n       return min; n     else if (c[i] < min) n       min = c[i]; n   } n } n int main() { n   read(n); n   for (int i = 1; i <= n; i++) read(c[i]); n   for (int i = 1; i <= n; i++) { n     read(nextt[i]); n     if (nextt[i] == i) vis[i] = -1, ans += c[i]; n   } n   for (int now = 1; now <= n; now++) { n     if (vis[now]) continue; n     for (int i = now;; i = nextt[i]) { n       if (vis[i]) { n         if (vis[i] == now) ans += MIN(i); n         break; n       } n       vis[i] = now; n     } n   } n   printf( %I64d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[200015], i, n1; n bool doit() { n   n1 = n - 1; n   for (i = 0; n1; ++i) { n     if (a[i] == a[i + 1]) { n       printf( :( ); n       return 0; n     } n     a[i] += n1; n     --n1; n   } n   for (i = 0; i < n; ++i) printf( %d  , a[i]); n } n int main() { n   scanf( %d , &n); n   i = n; n   while (i) { n     --i; n     scanf( %d , a + i); n     a[i] -= i; n   } n   sort(a, a + n); n   doit(); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, a, b; n   while (cin >> n >> m >> a >> b) { n     a--; n     b--; n     int r1 = a / m, r2 = b / m; n     int c1 = a % m, c2 = b % m; n     if (r1 == r2) n       cout << 1 << endl; n     else if (c1 == 0 && c2 == m - 1) n       cout << 1 << endl; n     else if (c1 == 0 && c2 != m - 1) { n       if (b == n - 1) n         cout << 1 << endl; n       else n         cout << 2 << endl; n     } else if (r1 + 1 == r2) n       cout << 2 << endl; n     else if (c1 != 0 && c2 == m - 1) n       cout << 2 << endl; n     else { n       if (b == n - 1) n         cout << 2 << endl; n       else if (c2 + 1 == c1) n         cout << 2 << endl; n       else n         cout << 3 << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct IP { n   string nm; n   int a, b, c, d; n }; n int n, m; n int test(IP a[], IP x) { n   for (int i = 0; i < n; i++) { n     if (a[i].a == x.a && a[i].b == x.b && a[i].c == x.c && a[i].d == x.d) n       return i; n   } n   return -1; n } n IP a[1000], b[1000]; n int main() { n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     cin >> a[i].nm >> a[i].a; n     getchar(); n     cin >> a[i].b; n     getchar(); n     cin >> a[i].c; n     getchar(); n     cin >> a[i].d; n   } n   for (int i = 0; i < m; i++) { n     cin >> b[i].nm >> b[i].a; n     getchar(); n     cin >> b[i].b; n     getchar(); n     cin >> b[i].c; n     getchar(); n     cin >> b[i].d; n     getchar(); n   } n   for (int i = 0; i < m; i++) { n     cout << b[i].nm <<    ; n     printf( %d.%d.%d.%d; , b[i].a, b[i].b, b[i].c, b[i].d); n     int k = test(a, b[i]); n     if (k != -1) cout <<   #  << a[k].nm; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n #pragma GCC target( sse4 ) n using namespace std; n template <class c> n struct rge { n   c b, e; n }; n template <class c> n rge<c> range(c i, c j) { n   return rge<c>{i, j}; n } n template <class c> n auto dud(c* x) -> decltype(cerr << *x, 0); n template <class c> n char dud(...); n struct debug { n   template <class c> n   debug& operator<<(const c&) { n     return *this; n   } n }; n map<string, long long> mp; n const long long N = 8; n long long a[N][N]; n long long ans1 = (long long)1e18 + 5, ans2 = 0; n vector<long long> vv(3); n vector<long long> ini = {0, 1, 2, 3, 4, 5, 6}; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long n; n   mp[ Anka ] = 0, mp[ Chapay ] = 1, mp[ Cleo ] = 2, mp[ Troll ] = 3, n   mp[ Dracul ] = 4, mp[ Snowy ] = 5, mp[ Hexadecimal ] = 6; n   cin >> n; n   string x, cc1, y; n   for (long long i = 0; i < n; i++) { n     cin >> x >> cc1 >> y; n     a[mp[x]][mp[y]] = 1; n   } n   cin >> vv[0] >> vv[1] >> vv[2]; n   do { n     for (long long i = 1; i < 5; i++) { n       for (long long j = i + 1; j < 6; j++) { n         vector<vector<long long> > aa(3); n         for (long long k = 0; k < i; k++) aa[0].push_back(ini[k]); n         for (long long k = i; k < j; k++) aa[1].push_back(ini[k]); n         for (long long k = j; k < 7; k++) aa[2].push_back(ini[k]); n         vector<vector<long long> > cur1 = aa; n         long long lik = 0; n         for (long long ii = 0; ii < 3; ii++) { n           for (long long jj = 0; jj < (long long)cur1[ii].size(); jj++) { n             for (long long k = jj + 1; k < (long long)cur1[ii].size(); k++) { n               if (a[cur1[ii][jj]][cur1[ii][k]]) { n                 lik++; n               } n               if (a[cur1[ii][k]][cur1[ii][jj]]) lik++; n             } n           } n         } n         vector<long long> cur = {0, 1, 2}; n         vector<long long> arr(3, 0); n         do { n           arr[0] = (vv[cur[0]] / (long long)aa[0].size()); n           arr[1] = (vv[cur[1]] / (long long)aa[1].size()); n           arr[2] = (vv[cur[2]] / (long long)aa[2].size()); n           long long ans = 0; n           for (long long ii = 0; ii < 3; ii++) { n             for (long long jj = ii + 1; jj < 3; jj++) { n               ans = max(ans, abs(arr[ii] - arr[jj])); n             } n           } n           if (ans <= ans1) { n             if (ans == ans1) { n               if (lik > ans2) { n                 ans2 = lik; n               } n             } else { n               ans1 = ans; n               ans2 = lik; n             } n           } n         } while (next_permutation((cur).begin(), (cur).end())); n       } n     } n   } while (next_permutation((ini).begin(), (ini).end())); n   cout << ans1 <<     << ans2 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   cin >> n >> k; n   long long ans = 1; n   for (int i = k + 1; i <= n; i++) ans = (ans * (n - k)) % 1000000007; n   ans = (ans * (k)) % 1000000007; n   if (k - 1 > 0) { n     long long prod = 0; n     vector<int> v(k + 2); n     int loopval = 1; n     for (int i = 1; i <= k - 1; i++) loopval *= k; n     for (int i = 0; i < loopval; i++) { n       int temp = i; n       for (int j = 0; j < k - 1; j++) { n         v[j + 2] = temp % k + 1; n         temp /= k; n       } n       int j = 0; n       for (; j < k - 1; j++) { n         int steps = 0; n         temp = v[j + 2]; n         while (steps <= 7 && temp > 1) { n           temp = v[temp]; n           steps++; n         } n         if (temp > 1) break; n       } n       if (j == k - 1) prod++; n     } n     ans = (ans * (prod)) % 1000000007; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int w; n   w = 1; n   while (w--) { n     int n, maxi = -1, b = 0; n     vector<int> s, t[10]; n     bool f1 = true, f = false; n     cin >> n; n     while (n > 0) { n       int x = n % 10; n       s.push_back(x); n       maxi = max(maxi, x); n       n /= 10; n     } n     reverse(s.begin(), s.end()); n     while (f1) { n       f1 = false; n       f = false; n       for (int i = 0; i < s.size(); i++) { n         int y = s[i]; n         if (y > 0) { n           f1 = true; n           f = true; n           t[b].push_back(1); n           y--; n           s[i] = y; n         } else { n           if (f) t[b].push_back(0); n         } n       } n       b++; n     } n     string ans[maxi]; n     cout << maxi << endl; n     for (int i = 0; i < maxi; i++) { n       for (int j = 0; j < t[i].size(); j++) { n         char c = (char)(t[i][j] +  0 ); n         ans[i].push_back(c); n       } n     } n     for (int i = 0; i < maxi; i++) cout << ans[i] <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 500010; n long long x, y, m; n int main() { n   ios_base::sync_with_stdio(false); n   cin >> x >> y >> m; n   if (x >= m || y >= m) { n     cout <<  0 n ; n     return 0; n   } n   if (x <= 0 && y <= 0) { n     cout <<  -1 n ; n     return 0; n   } n   if (x > y) swap(x, y); n   long long res = 0; n   if (x < 0) { n     res = ceil(-x * 1.0 / y); n     x += res * y; n   } n   while (x < m && y < m) { n     if (x < y) n       x = x + y; n     else n       y = x + y; n     res++; n   } n   cout << res <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Point { n   double x, y; n   Point() : x(0), y(0) {} n   Point(double x, double y) : x(x), y(y) {} n   Point(const Point &p) : x(p.x), y(p.y) {} n   int operator<(const Point &rhs) const { n     return make_pair(y, x) < make_pair(rhs.y, rhs.x); n   } n   int operator==(const Point &rhs) const { n     return make_pair(y, x) == make_pair(rhs.y, rhs.x); n   } n   Point operator+(const Point &p) const { return Point(x + p.x, y + p.y); } n   Point operator-(const Point &p) const { return Point(x - p.x, y - p.y); } n   Point operator*(double c) const { return Point(x * c, y * c); } n   Point operator/(double c) const { return Point(x / c, y / c); } n   double arg() const { return atan2(y, x); } n }; n double cross(Point p, Point q) { return p.x * q.y - p.y * q.x; } n double area(Point a, Point b, Point c) { n   return fabs(cross(a, b) + cross(b, c) + cross(c, a)) / 2; n } n double area2(Point a, Point b, Point c) { n   return cross(a, b) + cross(b, c) + cross(c, a); n } n double dot(Point p, Point q) { return p.x * q.x + p.y * q.y; } n double dist(Point p, Point q) { return sqrt(dot(p - q, p - q)); } n double dist2(Point p, Point q) { return dot(p - q, p - q); } n ostream &operator<<(ostream &os, const Point &p) { n   os <<  (  << p.x <<  ,  << p.y <<  ) ; n   return os; n } n Point offset(vector<Point> &v) { n   Point ans = v[0]; n   for (auto &p : v) { n     ans.x = min(ans.x, p.x); n     ans.y = min(ans.y, p.y); n   } n   return ans; n } n void diff(vector<Point> &v, vector<pair<double, Point>> &ret) { n   for (int i = 0; i < v.size(); ++i) { n     Point vec = v[(i + 1) % v.size()] - v[i]; n     ret.emplace_back(vec.arg(), vec); n   } n } n vector<Point> minkowskisum(vector<Point> &a, vector<Point> &b) { n   Point off = offset(a) + offset(b); n   vector<pair<double, Point>> v; n   vector<Point> ret; n   diff(a, v); n   diff(b, v); n   sort(v.begin(), v.end(), n        [&](const pair<double, Point> &x, const pair<double, Point> &y) { n          return x.first < y.first; n        }); n   vector<bool> mark(v.size(), false); n   for (int i = v.size() - 1; i > 0; --i) { n     if (abs(v[i].first - v[i - 1].first) < 1e-9) { n       mark[i] = true; n       v[i - 1].second = v[i - 1].second + v[i].second; n       v[i].second = Point(0, 0); n     } n   } n   for (int i = 0; i < v.size(); ++i) n     if (!mark[i]) ret.emplace_back(v[i].second); n   partial_sum(ret.begin(), ret.end(), ret.begin()); n   off = off - offset(ret); n   for (auto &p : ret) p = p + off; n   return ret; n } n double cross(Point a, Point b, Point c) { n   return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x); n } n bool PointInsidePolygon(vector<Point> &poly, Point q) { n   int a = 1, b = poly.size() - 1; n   swap(a, b); n   if (cross(poly[0], poly[a], q) > 0 || cross(poly[0], poly[b], q) < 0) n     return 0; n   while (abs(a - b) > 1) { n     int c = (a + b) >> 1; n     if (cross(poly[0], poly[c], q) > 0) { n       b = c; n     } else n       a = c; n   } n   return cross(poly[a], poly[b], q) <= 0; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   vector<Point> a[3]; n   for (int i = 0; i < 3; ++i) { n     int m; n     cin >> m; n     while (m--) { n       int x, y; n       cin >> x >> y; n       a[i].emplace_back(x, y); n     } n   } n   vector<Point> poly = minkowskisum(a[0], a[1]); n   poly = minkowskisum(poly, a[2]); n   int q; n   cin >> q; n   while (q--) { n     Point a; n     cin >> a.x >> a.y; n     a.x *= 3; n     a.y *= 3; n     if (PointInsidePolygon(poly, a)) n       cout <<  YES n ; n     else n       cout <<  NO n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   ; n   int n, w; n   cin >> n >> w; n   vector<long long> v(n); n   for (int i = 0; i < n; i++) cin >> v[i]; n   long long tot = 0, al = 0; n   long long minimum = 1e18, maximum = 0; n   for (int i = 0; i < n; i++) { n     tot += v[i]; n     minimum = min(minimum, tot); n     maximum = max(maximum, tot); n   } n   long long left = w - maximum; n   if (minimum < 0) al = abs(minimum); n   if (left < 0 || left < al) n     cout << 0; n   else n     cout << left - al + 1; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n int n; n double res; n double x[60006], v[60006]; n bool ok(double t) { n   double l, r; n   l = 1e-9; n   r = 1e9; n   for (int i = 1; i <= n; ++i) { n     double m = t * v[i]; n     l = max(l, x[i] - m); n     r = min(r, x[i] + m); n   } n   return (l <= r); n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) cin >> x[i]; n   for (int i = 1; i <= n; ++i) cin >> v[i]; n   double l, r, mid; n   l = 0.0, r = 1e9; n   while (abs(l - r) > 1e-6) { n     mid = (l + r) / 2; n     if (ok(mid)) { n       res = mid; n       r = mid; n     } else n       l = mid; n   } n   cout << fixed << setprecision(20) << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 500004, inf = 0x3f3f3f3f; n int a[MAX]; n int no[102]; n int nex[MAX][102]; n int f[102][102 * 102]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n, k, p; n   cin >> n >> k >> p; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     a[i] += a[i - 1]; n     a[i] %= p; n   } n   for (int i = 0; i < p; i++) no[i] = n + 1; n   for (int i = n; i >= 0; i--) { n     for (int j = 0; j < p; j++) { n       nex[i][j] = no[j]; n     } n     no[a[i]] = i; n   } n   int M = (p - 1) * k; n   memset(f, 0x3f, sizeof f); n   f[0][0] = 0; n   for (int i = 1; i <= k; i++) { n     for (int j = 0; j <= M; j++) { n       for (int u = min(p - 1, j); u >= 0; u--) { n         if (f[i - 1][j - u] <= n) n           f[i][j] = min(nex[f[i - 1][j - u]][j % p], f[i][j]); n       } n     } n   } n   for (int j = a[n]; j <= M; j += p) n     if (f[k][j] <= n) { n       cout << j; n       return 0; n     } n } n


#include <bits/stdc++.h> n using namespace std; n long double F(int A, int B) { n   long double S = 1; n   for (int i = 0; i < B; i++) S = S * (A - i) / (i + 1); n   return S; n } n int main() { n   int N; n   scanf( %d , &N); n   static int C[100000], Max[100000]; n   static long double P[100000][101]; n   memset(P, 0, sizeof(P)); n   for (int i = 0; i < N; i++) { n     scanf( %d , &C[i]); n     Max[i] = C[i]; n     P[i][C[i]] = 1; n   } n   int Query; n   scanf( %d , &Query); n   long double Ans = 0; n   for (int i = 0; i < N; i++) n     if (!C[i]) Ans++; n   while (Query--) { n     int a, b, K; n     scanf( %d%d%d , &a, &b, &K); n     a--; n     b--; n     static long double DP[101]; n     memset(DP, 0, sizeof(DP)); n     long double All = F(C[a], K); n     for (int i = 0; i <= C[a] && i <= Max[a]; i++) n       for (int j = max(0, K - C[a] + i); j <= K && j <= i; j++) n         DP[i - j] += P[a][i] * F(i, j) * F(C[a] - i, K - j) / All; n     Ans += DP[0] - P[a][0]; n     for (int i = 0; i <= Max[a]; i++) P[a][i] = DP[i]; n     C[a] -= K; n     C[b] += K; n     printf( %0.10lf n , (double)Ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     int input[n]; n     for (int i = (0); i < (n); i++) cin >> input[i]; n     sort(input, input + n); n     vector<int> ans(n); n     for (int i = (0); i < (n / 2); i++) { n       ans[i * 2] = i; n       ans[i * 2 + 1] = n - 1 - i; n     } n     if (n & 1) ans[n - 1] = n / 2; n     for (int i = (n)-1; i >= (0); --i) cout << input[ans[i]] <<    ; n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   char cell; n   short r, c, rr, cc, e; n   int res = 0, i, cat[100], cnt = 0; n   cin >> r >> c; n   rr = r; n   cc = c; n   for (i = 0; i < r; i++) { n     e = 0; n     for (int I = 0; I < c; I++) { n       cin >> cell; n       if (cell ==  S ) { n         e++; n         cat[cnt] = I; n         cnt++; n       } n     } n     if (e == 0) { n       res += c; n       rr--; n     } n   } n   cc -= cnt; n   sort(cat, cat + cnt); n   for (i = 1; i < cnt; i++) n     if (cat[i] == cat[i - 1]) cc++; n   res += cc * rr; n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b; n   cin >> a >> b; n   int answer1, answer2; n   answer1 = min(a, b); n   a -= answer1; n   b -= answer1; n   answer2 = (max(a, b)) / 2; n   cout << answer1 <<     << answer2; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5e5 + 3, M = 1e6 + 3, O = 2e6 + 3; n int c[N], a[N], b[N], id[O], tp; n bool f[N]; n struct P { n   int f, sz; n } d[M], st[O]; n int gf(int x) { return d[x].f == x ? x : gf(d[x].f); } n void mg(int x, int y) { n   int u = gf(x), v = gf(y); n   if (d[u].sz > d[v].sz) swap(u, v); n   st[++tp] = d[u], id[tp] = u, st[++tp] = d[v], id[tp] = v; n   d[v].sz += d[u].sz, d[u].f = v; n } n map<pair<int, int>, int> mp; n vector<int> vc[N]; n int main() { n   int n, m, k, i, j, l, u, v, w, uu, vv, tt, uuu, vvv; n   long long ans = 0; n   scanf( %d%d%d , &n, &m, &k); n   for (i = 1; i <= n; ++i) scanf( %d , c + i); n   for (i = 1; i <= n * 2; ++i) d[i].f = i, d[i].sz = 1; n   for (i = 1; i <= m; ++i) scanf( %d%d , a + i, b + i); n   for (i = 1; i <= m; ++i) n     if (c[a[i]] == c[b[i]]) { n       u = gf(a[i]), v = gf(b[i]); n       if (u == v) n         f[c[a[i]]] = 1; n       else n         mg(a[i], b[i] + n), mg(b[i], a[i] + n); n     } n   for (i = 1, j = 0; i <= m; ++i) n     if (c[a[i]] != c[b[i]] && !f[c[a[i]]] && !f[c[b[i]]]) { n       u = c[a[i]], v = c[b[i]]; n       if (u > v) swap(u, v); n       l = mp[{u, v}]; n       if (!l) l = mp[{u, v}] = ++j; n       vc[l].push_back(i); n     } n   for (auto o : mp) { n     u = o.first.first, v = o.first.second, w = o.second, tt = tp; n     for (int o2 : vc[w]) { n       uu = a[o2], vv = b[o2], uuu = gf(uu), vvv = gf(vv); n       if (uuu == vvv) { n         --ans; n         break; n       } n       mg(uu, vv + n), mg(uu + n, vv); n     } n     while (tp != tt) d[id[tp]] = st[tp], --tp; n   } n   for (i = 1, j = 0; i <= k; ++i) n     if (!f[i]) ++j; n   ans += j * 1ll * (j - 1) / 2; n   printf( %lld , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, sum = 0; n   string s; n   cin >> n; n   for (int i = 0; i < n; ++i) { n     cin >> s; n     if (s ==  Tetrahedron ) sum += 4; n     if (s ==  Cube ) sum += 6; n     if (s ==  Octahedron ) sum += 8; n     if (s ==  Dodecahedron ) sum += 12; n     if (s ==  Icosahedron ) sum += 20; n   } n   cout << sum; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int m, n; n   cin >> m >> n; n   char d[m][n]; n   int ub = m, lb = 0, lfb = n, rb = 0; n   for (int r = 0; r < m; r++) { n     for (int c = 0; c < n; c++) { n       cin >> d[r][c]; n       if (d[r][c] ==  * ) { n         if (ub > r) ub = r; n         if (lb < r) lb = r; n         if (rb < c) rb = c; n         if (lfb > c) lfb = c; n       } n     } n   } n   for (int r = ub; r <= lb; r++) { n     for (int c = lfb; c <= rb; c++) cout << d[r][c]; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   ; n   long long t, i, j, r, k, x, y, n, m, l, a, b, c, d = 0; n   cin >> t; n   string s, cm =  abacaba ; n   while (t--) { n     cin >> l >> r; n     if (2 * l <= r) { n       cout << l <<     << 2 * l <<   n ; n     } else n       cout <<  -1 -1 n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<string> s; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   s.resize(n); n   for (int i = 0; i < n; i++) { n     cin >> s[i]; n   } n   int ans = 0; n   for (int i = 0; i < 7; i++) { n     int cnt = 0; n     for (int j = 0; j < n; j++) { n       if (s[j][i] ==  1 ) cnt++; n     } n     ans = max(ans, cnt); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, char const *argv[]) { n   long long val; n   cin >> val; n   if (val & 1) n     cout << 1 << endl; n   else n     cout << 2 << endl; n   return 0; n } n


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


#include <bits/stdc++.h> n const int maxn = 1e6 + 5; n const int maxm = 1e5 + 5; n using namespace std; n int rd() { n   int x = 0; n   char c = getchar(); n   while (!isdigit(c)) c = getchar(); n   while (isdigit(c)) x = (x << 3) + (x << 1) + c -  0 , c = getchar(); n   return x; n } n int n, head[maxn], tot, a, b, c, d, cnt, Node, rk[maxn], len, lst[maxn], top; n int lena, lenb, lenc, lend, mx; n vector<int> vec[maxn]; n bitset<maxm> res; n struct node { n   int v, nxt; n } e[maxn << 1]; n void add(int u, int v) { n   e[++tot].v = v; n   e[tot].nxt = head[u]; n   head[u] = tot; n } n void dfs(int u, int y) { n   bool flag = true; n   for (int i = head[u]; i; i = e[i].nxt) { n     int v = e[i].v; n     if (v == y) continue; n     if (u == 1) ++cnt; n     flag = false; n     dfs(v, u); n   } n   if (flag) vec[cnt].push_back(u), rk[u] = cnt; n } n bool check() { n   lena = ((int)vec[rk[a]].size()); n   lenb = ((int)vec[rk[b]].size()); n   lenc = ((int)vec[rk[c]].size()); n   lend = ((int)vec[rk[d]].size()); n   for (int i = max(0, len - lena - lenb); i <= len - 2; i++) { n     if (lenc <= i && res[i - lenc]) return true; n     if (lend <= i && res[i - lend]) return true; n   } n   for (int i = max(0, len - lenc - lend); i <= len - 2; i++) { n     if (lena <= i && res[i - lena]) return true; n     if (lenb <= i && res[i - lenb]) return true; n   } n   return false; n } n int main() { n   n = rd(); n   a = rd(); n   b = rd(); n   c = rd(); n   d = rd(); n   for (int i = 2, u; i <= n; i++) { n     u = rd(); n     add(u, i); n     add(i, u); n   } n   dfs(1, 0); n   for (int i = 1; i <= cnt; i++) { n     Node += ((int)vec[i].size()); n     if (i != rk[a] && i != rk[b] && i != rk[c] && i != rk[d]) n       lst[++top] = ((int)vec[i].size()), mx = max(mx, ((int)vec[i].size())); n   } n   if (Node & 1) { n     puts( No ); n     return 0; n   } n   len = Node / 2 + 1; n   if (rk[a] == rk[b]) { n     puts( No ); n     return 0; n   } n   if (rk[c] == rk[d]) { n     puts( No ); n     return 0; n   } n   if (((int)vec[rk[a]].size()) > Node - len || n       ((int)vec[rk[b]].size()) > Node - len || n       ((int)vec[rk[c]].size()) > Node - len || n       ((int)vec[rk[d]].size()) > Node - len || mx > Node - len) { n     puts( No ); n     return 0; n   } n   res.set(0); n   for (int i = 1; i <= top; i++) res |= res << lst[i]; n   puts(check() ?  Yes  :  No ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[100005]; n int main() { n   int n, i; n   long long sum = 0, x = 0; n   cin >> n; n   for (i = 0; i < n; i++) { n     cin >> a[i]; n     sum += a[i]; n   } n   sort(a, a + n); n   for (i = 0; i < (n / 2); i++) { n     x += a[i]; n   } n   cout << x * x + (sum - x) * (sum - x) << endl; n   return 0; n } n


#include <bits/stdc++.h> n const int inf = 2 * 1e9; n const int mod = 1e9 + 7; n const long long infll = (long long)1e18; n const long double eps = 1e-9; n const int dx[] = {1, 0, -1, 0}; n const int dy[] = {0, -1, 0, 1}; n const int MAXY = 1005000; n using namespace std; n int n, m; n vector<vector<pair<int, int>>> g; n vector<bool> used; n vector<int> topsort; n void dfs(int v, int time) { n   used[v] = true; n   for (int i = 0; i < g[v].size(); i++) { n     int to = g[v][i].first; n     int edgeTime = g[v][i].second; n     if (edgeTime <= time) { n       if (!used[to]) dfs(to, time); n     } n   } n   topsort.push_back(v); n } n bool check(int time) { n   used.assign(n, false); n   topsort.clear(); n   for (int i = 0; i < n; i++) { n     if (!used[i]) dfs(i, time); n   } n   reverse(topsort.begin(), topsort.end()); n   for (int i = 0; i < (n - 1); i++) { n     int cur = topsort[i]; n     int nxt = topsort[i + 1]; n     bool ok = false; n     for (int j = 0; j < g[cur].size(); j++) { n       if (g[cur][j].second <= time) ok = ok || g[cur][j].first == nxt; n     } n     if (!ok) return false; n   } n   return true; n } n void solve() { n   cin >> n >> m; n   g.resize(n); n   used.resize(n); n   for (int i = 0; i < m; i++) { n     int x, y; n     cin >> x >> y; n     x--; n     y--; n     g[x].push_back(make_pair(y, i)); n   } n   if (!check(m - 1)) { n     cout << -1; n     return; n   } n   int tl = 0; n   int tr = m - 1; n   while (tr - tl > 1) { n     int tm = (tl + tr) / 2; n     if (check(tm)) n       tr = tm; n     else n       tl = tm; n   } n   cout << tr + 1; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int tt; n   cin >> tt; n   while (tt--) { n     int n, k; n     cin >> n >> k; n     vector<int> a(n); n     for (int i = 0; i < n; i++) { n       cin >> a[i]; n     } n     int d = unique(a.begin(), a.end()) - a.begin(); n     if (k > d) n       cout << 1 <<   n ; n     else if (k == 1) { n       if (d == 1) n         cout << 1 <<   n ; n       else n         cout << -1 <<   n ; n     } else { n       int ans = 1; n       d -= k; n       ans += (d + k - 2) / (k - 1); n       cout << ans <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dx[] = {-1, 1, 0, 0, -1, -1, 1, 1}; n int dy[] = {0, 0, 1, -1, -1, 1, -1, 1}; n int a[500], c[500]; n int main() { n   int t = 0, z = 0, len; n   int n, k = 0, m = 0, bam = 0, dan = 0, ans = 0; n   scanf( %d %d , &n, &m); n   for (int l = 1; l < n + 1; l++) { n     scanf( %d , &a[l]); n     a[l] += a[l - 1]; n   } n   for (int l = 0; l < m; l++) { n     int x, y; n     scanf( %d %d , &x, &y); n     c[l] = a[y] - a[x - 1]; n   } n   k = 0; n   sort(c, c + m); n   for (int l = m - 1; l > -1; l--) { n     k += c[l]; n     ans = max(ans, k); n   } n   printf( %d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using INT = long long; n int edge[2222][2222]; n int deg[2222]; n int n; n char s[2222]; n int id[2222]; n int ok() { n   for (int i = 1; i <= n; i++) id[i] = i; n   sort(id + 1, id + n + 1, [&](int u, int v) { return deg[u] < deg[v]; }); n   for (int i = 1, sum = 0; i < n; i++) { n     sum += deg[id[i]]; n     if (sum == i * (i - 1) / 2) return 0; n   } n   return 1; n } n int small_solve() { n   int mn = n + 1, cnt = 0; n   for (int i = 0; i < (1 << n); i++) { n     for (int j = 0; j < n; j++) n       if ((i >> j) & 1) { n         for (int k = 1; k <= n; k++) n           if (k != j + 1) { n             if (edge[k][j + 1]) n               deg[k]--, deg[j + 1]++; n             else n               deg[k]++, deg[j + 1]--; n             edge[k][j + 1] ^= 1; n             edge[j + 1][k] ^= 1; n           } n       } n     if (ok()) { n       if (mn > __builtin_popcount(i)) n         mn = __builtin_popcount(i), cnt = 1; n       else if (mn == __builtin_popcount(i)) n         cnt++; n     } n     for (int j = 0; j < n; j++) n       if ((i >> j) & 1) { n         for (int k = 1; k <= n; k++) n           if (k != j + 1) { n             if (edge[k][j + 1]) n               deg[k]--, deg[j + 1]++; n             else n               deg[k]++, deg[j + 1]--; n             edge[k][j + 1] ^= 1; n             edge[j + 1][k] ^= 1; n           } n       } n   } n   for (int i = 1; i <= mn; i++) cnt *= i; n   if (mn == n + 1) n     cout << -1 << endl; n   else n     cout << mn <<     << cnt << endl; n   exit(0); n } n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     scanf( %s , s + 1); n     for (int j = 1; j <= n; j++) n       if (s[j] ==  1 ) { n         edge[i][j] = 1; n         deg[i]++; n       } n   } n   if (ok()) { n     cout << 0 <<     << 1 << endl; n     return 0; n   } n   if (n <= 6) small_solve(); n   int cnt = 0; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) n       if (j != i) { n         if (edge[i][j]) n           deg[i]--, deg[j]++; n         else n           deg[i]++, deg[j]--; n       } n     if (ok()) cnt++; n     for (int j = 1; j <= n; j++) n       if (j != i) { n         if (!edge[i][j]) n           deg[i]--, deg[j]++; n         else n           deg[i]++, deg[j]--; n       } n   } n   cout << 1 <<     << cnt << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n const long long linf = 1e18 + 5; n long long int gcd(long long int a, long long int b) { n   return (b == 0 ? a : gcd(b, a % b)); n } n long long int lcm(int a, int b) { return (a * (b / gcd(a, b))); } n long long int max(long long int a, long long int b, long long int c) { n   return max(a, max(b, c)); n } n long long int power(long long int x, long long int y) { n   long long int ans = 1; n   while (y > 0) { n     if (y & 1) ans = (ans * x) % 1000000007; n     x = (x * x) % 1000000007; n     y /= 2; n   } n   return ans; n } n inline int read() { n   int ret = 0, temp = 1; n   int c = getchar(); n   while (c <  0  || c >  9 ) { n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     ret = (ret << 3) + (ret << 1) + c -  0 ; n     c = getchar(); n   } n   return ret; n } n string solve(int n) { n   string s; n   while (n > 0) { n     s = char(n % 10 + 48) + s; n     n /= 10; n   } n   return s; n } n double X[N], Y[N], A[N], B[N]; n double dist(pair<long long int, long long int> a, n             pair<long long int, long long int> b) { n   return sqrt((a.first - b.first) * (a.first - b.first) + n               (a.second - b.second) * (a.second - b.second)); n } n set<pair<double, int> > Adist, Bdist; n int main() { n   int n; n   double ax, ay, bx, by, tx, ty, x, y; n   cin >> ax >> ay >> bx >> by >> tx >> ty; n   cin >> n; n   double add = 0.0; n   for (int i = 0; i < n; i++) { n     cin >> x >> y; n     X[i] = x; n     Y[i] = y; n     add += 2 * dist({x, y}, {tx, ty}); n     A[i] = dist({x, y}, {ax, ay}) - dist({x, y}, {tx, ty}); n     B[i] = dist({x, y}, {bx, by}) - dist({x, y}, {tx, ty}); n     Adist.insert({A[i], i}); n     Bdist.insert({B[i], i}); n   } n   double onlyA = 0.0, onlyB = 0.0, bothAB = 0.0; n   onlyA = add + Adist.begin()->first; n   onlyB = add + Bdist.begin()->first; n   bothAB = add; n   if (n == 1) { n     printf( %.10lf n , min(onlyA, onlyB)); n   } else { n     if (Adist.begin()->second == Bdist.begin()->second) { n       auto it1 = Adist.begin(); n       it1++; n       auto it2 = Bdist.begin(); n       it2++; n       double ans = min(Adist.begin()->first + it2->first, n                        Bdist.begin()->first + it1->first); n       bothAB += ans; n     } else n       bothAB += (Adist.begin()->first + Bdist.begin()->first); n     printf( %.10lf , min(min(onlyA, onlyB), bothAB)); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1000000007; n const long long MAX = 200005; n bool prime[MAX]; n void sieve() { n   memset(prime, true, sizeof(prime)); n   prime[0] = prime[1] = false; n   for (long long p = 2; p * p <= MAX; p++) { n     if (prime[p] == true) { n       for (long long i = p * 2; i <= MAX; i += p) prime[i] = false; n     } n   } n } n long long power(long long a, long long b, long long m) { n   if (b == 0) return 1; n   if (b == 1) return a % m; n   long long t = power(a, b / 2, m); n   t = (t * t) % m; n   if (b & 1) t = (t * a) % m; n   return t; n } n long long modInverse(long long a, long long m) { return power(a, m - 2, m); } n long long __gcd(long long a, long long b) { n   if (b == 0) return a; n   return __gcd(b, a % b); n } n bool cmp(const pair<long long, long long> &l, n          const pair<long long, long long> &r) { n   return l.first < r.first; n } n long long n, q; n vector<vector<long long> > g; n long long visited[MAX]; n long long num[MAX]; n long long cnt[MAX]; n long long k; n long long dfs(long long node) { n   visited[node] = 1; n   num[node] = k; n   k++; n   long long p = 0, flag = 0; n   for (auto i : g[node]) { n     if (!visited[i]) { n       flag = 1; n       p += dfs(i); n     } n   } n   if (flag) { n     cnt[node] = p; n     return 1 + p; n   } else { n     cnt[node] = 0; n     return 1; n   } n } n void solve() { n   cin >> n >> q; n   g.resize(n + 1, vector<long long>()); n   long long x; n   for (long long i = 2; i <= n; i++) { n     cin >> x; n     g[i].push_back(x); n     g[x].push_back(i); n   } n   memset(visited, 0, sizeof(visited)); n   memset(num, 0, sizeof(num)); n   memset(cnt, 0, sizeof(cnt)); n   k = 1; n   dfs(1); n   map<long long, long long> m; n   for (long long i = 0; i < MAX; i++) m[num[i]] = i; n   while (q--) { n     long long x, y; n     cin >> x >> y; n     if (cnt[x] < y - 1) n       cout <<  -1  n            <<   n ; n     else { n       long long w = num[x]; n       cout << m[w + y - 1] <<   n ; n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   ; n   long long t; n   t = 1; n   while (t--) { n     solve(); n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> v; n int main() { n   long long a, b, f, k; n   cin >> a >> b >> f >> k; n   if (b / a >= k) { n     cout << 0 << endl; n     return 0; n   } n   v.push_back(f); n   for (long long i = 2;; i += 2) { n     long long r1 = i * a - f; n     long long r2 = i * a + f; n     if (r1 > k * a) break; n     if (r1 <= k * a) v.push_back(r1); n     if (r2 <= k * a) v.push_back(r2); n   } n   v.push_back(k * a); n   if (b < f) { n     cout << -1; n     return 0; n   } n   long long last = 0; n   long long has = b; n   int sum = 0; n   for (int i = 0; i < v.size(); i++) { n     long long dis = v[i] - last; n     if (dis > has) { n       has = b; n       sum++; n     } n     if (has < dis) { n       cout << -1 << endl; n       return 0; n     } n     has -= dis; n     last = v[i]; n   } n   cout << sum << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[1000005]; n int main() { n   int n; n   long long sum = 0; n   cin >> n; n   memset(a, -1, sizeof(a)); n   for (int i = n; i >= 0; i--) { n     if (a[i] != -1) n       continue; n     else { n       for (int tt = 1;; tt *= 2) { n         if (tt - 1 >= i) { n           a[i] = tt - 1 - i; n           a[tt - 1 - i] = i; n           sum = sum + 2 * (tt - 1); n           break; n         } n       } n     } n   } n   cout << sum << endl; n   for (int i = 0; i <= n; i++) { n     if (i != 0) printf(   ); n     printf( %d , a[i]); n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int a; n   map<int, int> mp; n   int mx = 0; n   for (int i = 0; i < n; i++) { n     cin >> a; n     mp[a]++; n   } n   for (auto it : mp) { n     mx = max(mx, it.second); n   } n   cout << n - mx; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1); n const int LIM = 15e5; n struct Vector { n   int x, y; n   Vector operator+(Vector v) { return {x + v.x, y + v.y}; } n   Vector operator-(Vector v) { return {x - v.x, y - v.y}; } n   long long operator%(Vector v) { return 1LL * x * v.y - 1LL * v.x * y; } n   long long operator*(Vector v) { return 1LL * x * v.x + 1LL * y * v.y; } n   friend double angle(Vector va, Vector vb) { n     return abs(atan2(va % vb, va * vb) * 180 / PI); n   } n   double len() { return hypot(x, y); } n   Vector operator*(int k) { return {k * x, k * y}; } n }; n const int N = 1e5 + 1; n int n, sig[N]; n Vector a[N]; n vector<int> s[N]; n void combine(int p) { n   for (int i = p; i <= p + 2; ++i) n     for (int j = i + 1; j <= p + 2; ++j) { n       if (angle(a[i], a[j]) >= 120) { n         int r = 3 * p + 3 - i - j; n         if (s[i].size() > s[j].size()) s[i].swap(s[j]); n         s[j].insert(s[j].end(), s[i].begin(), s[i].end()); n         a[j] = a[i] + a[j]; n         if (r == p) { n           s[i].swap(s[r]); n           a[i] = a[r]; n         } n         return; n       } else if (angle(a[i], a[j]) <= 60) { n         int r = 3 * p + 3 - i - j; n         for (int id : s[j]) sig[id] *= -1; n         if (s[i].size() > s[j].size()) s[i].swap(s[j]); n         s[j].insert(s[j].end(), s[i].begin(), s[i].end()); n         a[j] = a[i] - a[j]; n         if (r == p) { n           s[i].swap(s[r]); n           a[i] = a[r]; n         } n         return; n       } n     } n } n bool toolong(Vector v) { return v.len() > LIM; } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     cin >> a[i].x >> a[i].y; n     s[i].push_back(i); n     sig[i] = 1; n   } n   if (n == 1) return cout << 1, 0; n   for (int i = 1; i <= n - 2; ++i) { n     combine(i); n   } n   if (angle(a[n - 1], a[n]) <= 90) { n     for (int id : s[n]) sig[id] *= -1; n   } n   for (int i = 1; i <= n; ++i) { n     cout << sig[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[200005]; n int func(long a, long long x, long long f) { n   if (a - x > 0) { n     int tem; n     tem = (a - x) / (f + x); n     if (tem * (f + x) == (a - x)) n       return (a - x) / (f + x); n     else n       return (a - x) / (f + x) + 1; n   } else n     return 0; n } n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[i]; n   long long x, f; n   cin >> x >> f; n   long long ans = 0; n   for (int i = 0; i < n; i++) { n     int tem = func(a[i], x, f); n     ans += tem; n   } n   cout << ans * f << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n void rset(); n void init_test(); n void solve(); n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout << fixed; n   cout.precision(20); n   init_test(); n   return 0; n } n template <typename T> n void chmin(T& a, T b) { n   if (a > b) a = b; n } n template <typename T> n void chmax(T& a, T b) { n   if (a < b) a = b; n } n template <typename T> n void MACRO_rdv2_Init(long long n, T& t) { n   t.resize(n); n } n template <typename First, typename... Rest> n void MACRO_rdv2_Init(long long n, First& first, Rest&... rest) { n   first.resize(n); n   MACRO_rdv2_Init(n, rest...); n } n template <typename T> n void MACRO_rdv2_Scan(long long p, T& t) { n   std::cin >> t[p]; n } n template <typename First, typename... Rest> n void MACRO_rdv2_Scan(long long p, First& first, Rest&... rest) { n   std::cin >> first[p]; n   MACRO_rdv2_Scan(p, rest...); n } n template <typename T> n void wrv(const vector<T>& v) { n   for (long long(__ii) = (0); (__ii) < (((long long)v.size())); ++(__ii)) { n     if (__ii) cout <<    ; n     cout << v[__ii]; n   } n   cout <<   n ; n } n template <typename T> n void wrm(const vector<vector<T>>& v) { n   for (long long(__ii) = (0); (__ii) < (((long long)v.size())); ++(__ii)) { n     for (long long(__jj) = (0); (__jj) < (v[__ii].size()); ++(__jj)) { n       if (__jj) cout <<    ; n       cout << v[__ii][__jj]; n     } n     cout <<   n ; n   } n } n template <typename T> n void sc(T& x) { n   cin >> x; n } n template <typename Head, typename... Tail> n void sc(Head& head, Tail&... tail) { n   cin >> head; n   sc(tail...); n } n template <typename T> n void wr(const T& x) { n   cout << x <<   n ; n } n template <typename Head, typename... Tail> n void wr(const Head& head, const Tail&... tail) { n   cout << head <<    ; n   wr(tail...); n } n template <typename T> n void wrf(const T& x) { n   cout << x << endl; n } n template <typename Head, typename... Tail> n void wrf(const Head& head, const Tail&... tail) { n   cout << head <<    ; n   wrf(tail...); n } n template <typename T> n void debug_out(const T& x) { n   cerr << x <<   n ; n } n template <typename Head, typename... Tail> n void debug_out(const Head& head, const Tail&... tail) { n   cerr << head <<    ; n   debug_out(tail...); n } n template <typename... T> n void err(const T&... cod) { n   wr(cod...); n   exit(0); n } n void solve() { n   long long n; n   sc(n); n   vector<long long> a(n); n   for (long long(__ii) = (0); (__ii) < (n); ++(__ii)) cin >> a[__ii]; n   if (accumulate(a.begin(), a.end(), 0LL) % n != 0) { n     wr(-1); n     return; n   }; n   long long s = accumulate(a.begin(), a.end(), 0LL) / n; n   vector<pair<pair<long long, long long>, long long>> ans; n   for (long long(i) = (1); (i) < (n); ++(i)) { n     if (a[i] % (i + 1)) { n       long long k = (i + 1) - (a[i] % (i + 1)); n       ans.push_back(make_pair(make_pair(1, i + 1), k)); n       a[i] += k; n       a[0] -= k; n     } n     long long k = a[i] / (i + 1); n     ans.push_back(make_pair(make_pair(i + 1, 1), k)); n     a[0] += k * (i + 1); n     a[i] -= k * (i + 1); n   } n   for (long long(i) = (1); (i) < (n); ++(i)) { n     ans.push_back(make_pair(make_pair(1, i + 1), s)); n     a[0] -= s; n     a[i] += s; n   } n   wr(((long long)ans.size())); n   for (long long(i) = (0); (i) < (((long long)ans.size())); ++(i)) { n     wr(ans[i].first.first, ans[i].first.second, ans[i].second); n   } n } n void init_test() { n   long long qq = 1; n   cin >> qq; n   while (qq--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 500100; n long long n, m, de, dt, h; n int main() { n   int a, b, c; n   int x, y, z; n   cin >> a >> b >> c; n   cin >> x >> y >> z; n   int m1 = -(x - a); n   int m2 = -(y - b); n   int m3 = -(z - c); n   int neg = 0, pos = 0; n   if (m1 < 0) n     neg += m1; n   else n     pos += m1 - m1 % 2; n   if (m2 < 0) n     neg += m2; n   else n     pos += m2 - m2 % 2; n   if (m3 < 0) n     neg += m3; n   else n     pos += m3 - m3 % 2; n   if (-neg * 2 > pos) n     printf( No n ); n   else n     printf( Yes n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3e5 + 10; n vector<int> g[N]; n int c[N], val[N], ans[N]; n void merge(vector<int> &vc, vector<int> &tmp) { n   for (auto u : tmp) vc.push_back(u); n } n vector<int> dfs(int u) { n   vector<int> vc, tmp; n   for (auto v : g[u]) { n     tmp = dfs(v); n     merge(vc, tmp); n   } n   if (c[u] > vc.size()) { n     printf( NO n ); n     exit(0); n   } n   vc.insert(vc.begin() + c[u], u); n   return vc; n } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1, u; i <= n; i++) { n     val[i] = 1; n     scanf( %d%d , &u, &c[i]); n     g[u].push_back(i); n   } n   vector<int> vc = dfs(0); n   for (int i = 0; i < vc.size(); i++) { n     ans[vc[i]] = i + 1; n   } n   printf( YES n ); n   for (int i = 1; i <= n; i++) printf( %d  , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e9, SIZE = 1e5 * 2; n vector<long long> vec; n long long dp[100][SIZE], tree[SIZE * 8], pushing[SIZE * 8], prevPos[SIZE], n     last[SIZE]; n void push(int v) { n   tree[v] += pushing[v]; n   pushing[v * 2 + 1] += pushing[v]; n   pushing[v * 2 + 2] += pushing[v]; n   pushing[v] = 0; n } n void build(int v, int l, int r, int k) { n   if (l == r - 1) { n     tree[v] = dp[k][l]; n   } else { n     int mid = (l + r) / 2; n     build(v * 2 + 1, l, mid, k); n     build(v * 2 + 2, mid, r, k); n     tree[v] = max(tree[v * 2 + 1], tree[v * 2 + 2]); n   } n } n void upd(int v, int l, int r, int askl, int askr) { n   push(v); n   if (l >= askr || r <= askl) return; n   if (l >= askl && r <= askr) { n     pushing[v]++; n     push(v); n     return; n   } n   int mid = (l + r) / 2; n   upd(v * 2 + 1, l, mid, askl, askr); n   upd(v * 2 + 2, mid, r, askl, askr); n   tree[v] = max(tree[v * 2 + 1], tree[v * 2 + 2]); n } n long long get(int v, int l, int r, int askl, int askr) { n   push(v); n   if (l >= askr || r <= askl) return 0; n   if (l >= askl && r <= askr) { n     return tree[v]; n   } n   int mid = (l + r) / 2; n   return max(get(v * 2 + 1, l, mid, askl, askr), n              get(v * 2 + 2, mid, r, askl, askr)); n } n int main() { n   cin.tie(0); n   cout.tie(0); n   ios_base::sync_with_stdio(0); n   ; n   long long n, k; n   cin >> n >> k; n   vec.resize(n); n   for (int i = 0; i < n; i++) cin >> vec[i]; n   set<long long> vals; n   long long ans = 0; n   for (int i = 0; i <= n; i++) { n     last[i] = 0; n   } n   for (int i = 0; i < n; i++) { n     vals.insert(vec[i]); n     dp[0][i] = vals.size(); n     prevPos[i] = last[vec[i]]; n     last[vec[i]] = i; n     ans = max(ans, dp[0][i]); n   } n   for (int j = 1; j < k; j++) { n     for (int i = 0; i < SIZE; i++) { n       tree[i] = 0; n       pushing[i] = 0; n     } n     build(0, 0, n, j - 1); n     for (int i = 0; i < n; i++) { n       upd(0, 0, n, prevPos[i], i); n       dp[j][i] = max(dp[j - 1][i], get(0, 0, n, 0, i)); n       ans = max(ans, dp[j][i]); n     } n     cout <<   ; n   } n   cout << dp[k - 1][n - 1]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long k; n   string n; n   cin >> k >> n; n   long long sum = 0; n   vector<long long> arr; n   for (int(i) = 0; (i) < ((int)(n).size()); (i)++) { n     sum += int(n[i] -  0 ); n     arr.push_back(int(n[i] -  0 )); n   } n   if (sum >= k) { n     printf( 0 ); n     return 0; n   } else { n     sort((arr).begin(), (arr).end()); n     long long col = 0; n     for (int(i) = 0; (i) < ((int)(arr).size()); (i)++) { n       sum -= arr[i]; n       sum += 9; n       col++; n       if (sum >= k) { n         printf( %lld , col); n         return 0; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n const int INF = 0x3f3f3f3f; n const int maxn = 1e5 + 5; n const int mod = 1e9 + 7; n const double eps = 1e-7; n using namespace std; n long long ans, n, s, a[maxn], b[maxn], num1, num2; n priority_queue<pair<long long, long long>, vector<pair<long long, long long> >, n                greater<pair<long long, long long> > > n     to1, to2; n void init() { n   ans = 0; n   num1 = 0; n   num2 = 0; n   while (to1.size()) to1.pop(); n   while (to2.size()) to2.pop(); n   memset(a, 0, sizeof(a)), memset(b, 0, sizeof(b)); n } n int main() { n   while (~scanf( %lld%lld , &n, &s)) { n     init(); n     for (int i = 0; i <= n - 1; ++i) { n       long long s, a, b; n       scanf( %lld%lld%lld , &s, &a, &b); n       if (a > b) { n         num1 += s; n         ans += s * a; n         to2.push(pair<long long, long long>(a - b, s)); n       } else { n         num2 += s; n         ans += s * b; n         to1.push(pair<long long, long long>(b - a, s)); n       } n     } n     num1 %= s, num2 %= s; n     if (num1 + num2 > s) { n       printf( %lld n , ans); n       continue; n     } else { n       long long tmp1 = num1, tmp2 = num2, cost1 = 0, cost2 = 0; n       while (tmp1) { n         pair<long long, long long> tmp = to2.top(); n         to2.pop(); n         long long num = min(tmp1, tmp.second); n         tmp1 -= num; n         cost1 += num * tmp.first; n       } n       while (tmp2) { n         pair<long long, long long> tmp = to1.top(); n         to1.pop(); n         int num = min(tmp2, tmp.second); n         tmp2 -= num; n         cost2 += num * tmp.first; n       } n       printf( %lld n , max(ans - cost1, ans - cost2)); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma comment(linker,  /STACK:102400000,102400000 ) n long long n, k, m, d; n int main() { n   cin >> n >> k >> m >> d; n   long long ans = min(n, m); n   for (long long i = 1; i <= d; i++) { n     long long tmp = (i - 1) * k + 1; n     if (tmp <= 0) continue; n     long long x = n / tmp; n     if (x > m) x = m; n     if (x * (i - 1) > (n - x) / k) continue; n     long long res = x * (i - 1) + x; n     if (res > ans) { n       ans = res; n     } n   } n   cout << ans << endl; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int mx = 2e5 + 10; n int a[mx]; n vector<int> g[mx]; n vector<int> lyr[mx]; n bool cmp(int i, int j) { return g[i].size() < g[j].size(); } n int main() { n   int n, t, k; n   scanf( %d %d %d , &n, &t, &k); n   a[0] = 1; n   for (int i = 1; i <= t; i++) scanf( %d , a + i); n   int from = 0; n   int leafs = a[t]; n   for (int i = 0; i < t; i++) { n     int to = from + a[i] - 1; n     int l = to + 1; n     int r = l + a[i + 1] - 1; n     for (int x = 0; x < min(a[i], a[i + 1]); x++) g[from + x].push_back(l + x); n     if (a[i] < a[i + 1]) { n       for (int x = 0; x < a[i + 1] - a[i]; x++) { n         g[to].push_back(r - x); n       } n     } else n       leafs += a[i] - a[i + 1]; n     for (int x = from; x <= to; x++) lyr[i].push_back(x); n     sort(lyr[i].begin(), lyr[i].end(), cmp); n     from = l; n   } n   for (int i = 1; i < t; i++) { n     int to = lyr[i].back(); n     for (int u : lyr[i]) n       if (u != to && !g[u].empty() && leafs < k) { n         for (int v : g[u]) g[to].push_back(v); n         g[u].clear(); n         leafs++; n       } n   } n   if (leafs != k) { n     puts( -1 ); n     return 0; n   } n   printf( %d n , n); n   for (int i = 0; i < n; i++) n     for (int j : g[i]) printf( %d %d n , i + 1, j + 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string a, b; n   cin >> a >> b; n   int res = -1, pos = -1, j = 0; n   for (int i = 0; i < b.size(); i++) { n     if (a[j] != b[i]) { n       pos = j; n       if (res != -1 || a[j + 1] != b[i]) { n         res = 0; n         break; n       } n       res = 1; n       j++; n     } n     j++; n   } n   if (res == -1) res = 1, pos = a.size() - 1; n   if (res) { n     vector<int> v; n     v.push_back(pos); n     while (pos > 0 && a[--pos] == a[v[0]]) v.push_back(pos); n     cout << v.size() << endl; n     for (int i = v.size() - 1; i >= 0; i--) cout << v[i] + 1 <<    ; n     cout << endl; n   } else n     cout << 0 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   vector<int> v; n   int n, A, B; n   cin >> n >> A >> B; n   int msum = 0; n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     v.push_back(x); n     msum += x; n   } n   int one = v[0]; n   v[0] = INT_MAX; n   sort(v.begin(), v.end()); n   int x = n; n   int sum = msum; n   int res = 0; n   for (int i = n - 2; i >= 0; i--) { n     if (((one * A) / sum) >= B) { n       cout << res << endl; n       return 0; n     } n     sum -= v[i]; n     res++; n   } n   cout << n - 1 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, nex[2000000], hea[2000000], wen[2000000], val[2000000], aid[2000000], n     root2, root1, len, maxx; n struct segment_tree { n   int a[2000000][2], lazy[2000000], fa[1000000], dep[1000000], in[1000000], n       out[1000000], m; n   void pushdown(int k) { n     if (!lazy[k]) return; n     swap(a[k << 1][0], a[k << 1][1]); n     swap(a[(k << 1) | 1][0], a[(k << 1) | 1][1]); n     lazy[k << 1] ^= 1; n     lazy[(k << 1) | 1] ^= 1; n     lazy[k] = 0; n   } n   void update1(int l, int r, int k, int x, int y, int z) { n     if (l == r) { n       a[k][z] = y; n       return; n     } n     int mid = (l + r) >> 1; n     if (x <= mid) update1(l, mid, k << 1, x, y, z); n     if (x > mid) update1(mid + 1, r, (k << 1) | 1, x, y, z); n     a[k][0] = max(a[k << 1][0], a[(k << 1) | 1][0]); n     a[k][1] = max(a[k << 1][1], a[(k << 1) | 1][1]); n   } n   void update2(int l, int r, int k, int x, int y) { n     if (l >= x && r <= y) { n       lazy[k] ^= 1; n       swap(a[k][0], a[k][1]); n       return; n     } n     pushdown(k); n     int mid = (l + r) >> 1; n     if (x <= mid) update2(l, mid, k << 1, x, y); n     if (y > mid) update2(mid + 1, r, (k << 1) | 1, x, y); n     a[k][0] = max(a[k << 1][0], a[(k << 1) | 1][0]); n     a[k][1] = max(a[k << 1][1], a[(k << 1) | 1][1]); n   } n   void build(int x, int y, int z) { n     int a = 0; n     in[x] = ++m; n     dep[x] = dep[y] + 1; n     for (int i = hea[x]; i; i = nex[i]) n       if (wen[i] != y) { n         ++a; n         fa[aid[i]] = wen[i]; n         build(wen[i], x, z ^ val[i]); n       } n     update1(1, n, 1, in[x], dep[x], z); n     out[x] = m; n   } n   void revers(int x) { update2(1, n, 1, in[fa[x]], out[fa[x]]); } n } st1, st2; n void add(int x, int y, int z, int p) { n   ++len; n   nex[len] = hea[x]; n   wen[len] = y; n   val[len] = z; n   aid[len] = p; n   hea[x] = len; n } n void dfs(int x, int y, int z) { n   if (z >= maxx) maxx = z, root1 = x; n   for (int i = hea[x]; i; i = nex[i]) n     if (wen[i] != y) dfs(wen[i], x, z + 1); n } n void dfs1(int x, int y, int z) { n   if (z >= maxx) maxx = z, root2 = x; n   for (int i = hea[x]; i; i = nex[i]) n     if (wen[i] != y) dfs1(wen[i], x, z + 1); n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i < n; i++) { n     int x, y, z; n     scanf( %d%d%d , &x, &y, &z); n     add(x, y, z, i); n     add(y, x, z, i); n   } n   dfs(1, 0, 0); n   dfs1(root1, 0, 0); n   st1.build(root1, 0, 0); n   st2.build(root2, 0, 0); n   scanf( %d , &m); n   for (int i = 1; i <= m; i++) { n     int x; n     scanf( %d , &x); n     st1.revers(x); n     st2.revers(x); n     printf(  %d n , max(st1.a[1][0], st2.a[1][0]) - 1); n   } 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   int a = 0, b = 1023, c, n; n   char op; n   cin >> n; n   for (int i = 0; i < n; ++i) { n     cin >> op >> c; n     switch (op) { n       case  | : n         a |= c; n         b |= c; n         break; n       case  & : n         a &= c; n         b &= c; n         break; n       case  ^ : n         a ^= c; n         b ^= c; n         break; n     } n   } n   int r = 0, nd = 1023, xr = 0; n   for (int i = 0; i < 10; ++i) { n     int p = (a >> i) & 1; n     int q = (b >> i) & 1; n     if (!p && !q) { n       nd ^= (1 << i); n     } else if (p && !q) { n       xr |= (1 << i); n     } else if (p && q) { n       r |= (1 << i); n     } n   } n   int s = 3; n   if (nd == 1023) s--; n   if (xr == 0) s--; n   if (r == 0) s--; n   cout << s << endl; n   if (nd != 1023) cout <<  &   << nd << endl; n   if (xr != 0) cout <<  ^   << xr << endl; n   if (r != 0) cout <<  |   << r << endl; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   std::ios_base::sync_with_stdio(false); n   std::string s; n   std::cin >> s; n   int n = s.length(); n   std::vector<int> prefix_fun(n, 0); n   for (int i = 1; i < n; i++) { n     int j = prefix_fun[i - 1]; n     while (j > 0 && s[i] != s[j]) { n       j = prefix_fun[j - 1]; n     } n     if (s[i] == s[j]) { n       j++; n     } n     prefix_fun[i] = j; n   } n   if (prefix_fun[n - 1] == 0) { n     std::cout <<  Just a legend ; n     return 0; n   } n   for (int i = 1; i < n - 1; i++) { n     if (prefix_fun[i] == prefix_fun[n - 1]) { n       std::cout << s.substr(0, prefix_fun[i]); n       return 0; n     } n   } n   int j = prefix_fun[prefix_fun[n - 1] - 1]; n   if (j == 0) { n     std::cout <<  Just a legend ; n     return 0; n   } n   std::cout << s.substr(0, j); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e3 + 7, M = 1e6 + 7; n int row[N], col[N]; n long long dpc[M], dpr[M]; n int main() { n   priority_queue<int> pqc, pqr; n   int n, m, k, p; n   scanf( %d %d %d %d , &n, &m, &k, &p); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       int a; n       scanf( %d , &a); n       row[i] += a; n       col[j] += a; n       if (i == n - 1) { n         pqc.push(col[j]); n       } n     } n     pqr.push(row[i]); n   } n   for (int i = 1; i <= k; i++) { n     dpr[i] = pqr.top() + dpr[i - 1]; n     pqr.push(pqr.top() - p * m); n     pqr.pop(); n     dpc[i] = pqc.top() + dpc[i - 1]; n     pqc.push(pqc.top() - p * n); n     pqc.pop(); n   } n   long long ans = -1e18; n   for (int i = 0; i <= k; i++) { n     ans = max(ans, 1ll * dpr[i] + dpc[k - i] - (1ll * i * (k - i) * p)); n   } n   printf( %lld , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     long long int n, x; n     cin >> n >> x; n     long long int arr[n]; n     for (int i = 0; i < n; i++) cin >> arr[i]; n     sort(arr, arr + n); n     long double rich_sum = 0; n     long long int poor_idx = -1; n     for (int i = 0; i < n; i++) { n       if (arr[i] < x) { n         poor_idx = i; n       } else n         rich_sum += arr[i]; n     } n     int i; n     for (i = poor_idx; i >= 0; i--) { n       rich_sum += arr[i]; n       if (rich_sum / (n - i) < x) break; n     } n     int poor_cnt = i + 1; n     cout << (n - poor_cnt) << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200005; n const int INF = (int)2e9 + 10; n typedef struct vocationn { n   int min_cost[N]; n   vector<pair<int, int> > left[N]; n   vector<pair<int, int> > right[N]; n } vocation; n int n, x, cost, l, r, ans = INF; n vocation voc; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   for (int i = 0; i < N; i++) voc.min_cost[i] = INF; n   cin >> n >> x; n   for (size_t i = 0; i < n; i++) { n     cin >> l >> r >> cost; n     voc.left[l].push_back(pair<int, int>(r - l + 1, cost)); n     voc.right[r].push_back(pair<int, int>(r - l + 1, cost)); n   } n   for (int i = 0; i < N; i++) { n     for (pair<int, int> t : voc.left[i]) { n       if (t.first >= x) continue; n       if (voc.min_cost[x - t.first] < INF) n         ans = min(ans, voc.min_cost[x - t.first] + t.second); n     } n     for (pair<int, int> t : voc.right[i]) n       voc.min_cost[t.first] = min(voc.min_cost[t.first], t.second); n   } n   if (ans == INF) n     cout <<  -1  << endl; n   else n     cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n void readi(T &x) { n   T input = 0; n   bool negative = false; n   char c =    ; n   while (c <  - ) { n     c = getchar(); n   } n   if (c ==  - ) { n     negative = true; n     c = getchar(); n   } n   while (c >=  0 ) { n     input = input * 10 + (c -  0 ); n     c = getchar(); n   } n   if (negative) { n     input = -input; n   } n   x = input; n } n template <class T> n void printi(T output) { n   if (output == 0) { n     putchar( 0 ); n     return; n   } n   if (output < 0) { n     putchar( - ); n     output = -output; n   } n   int aout[20]; n   int ilen = 0; n   while (output) { n     aout[ilen] = ((output % 10)); n     output /= 10; n     ilen++; n   } n   for (int i = ilen - 1; i >= 0; i--) { n     putchar(aout[i] +  0 ); n   } n   return; n } n template <class T> n void ckmin(T &a, T b) { n   a = min(a, b); n } n template <class T> n void ckmax(T &a, T b) { n   a = max(a, b); n } n long long randomize(long long mod) { n   return ((1ll << 30) * rand() + (1ll << 15) * rand() + rand()) % mod; n } n const long double PI = 4.0 * atan(1.0); n const long double EPS = 1e-10; n long long normalize(long long x, long long mod = 1000000007) { n   return (((x % mod) + mod) % mod); n } n int Q; n int X[3]; n vector<int> factors[100013]; n int A, B, C; n vector<int> guy[8]; n int need[8][8][8]; n int sz[8], pref[8]; n int p[6][3] = {{0, 1, 2}, {0, 2, 1}, {1, 0, 2}, n                {1, 2, 0}, {2, 0, 1}, {2, 1, 0}}; n bool works(int a, int b, int c) { n   if ((a & 1) && (b & 2) && (c & 4)) { n     return true; n   } n   if ((a & 1) && (b & 4) && (c & 2)) { n     return true; n   } n   if ((a & 2) && (b & 1) && (c & 4)) { n     return true; n   } n   if ((a & 2) && (b & 4) && (c & 1)) { n     return true; n   } n   if ((a & 4) && (b & 1) && (c & 2)) { n     return true; n   } n   if ((a & 4) && (b & 2) && (c & 1)) { n     return true; n   } n   return false; n } n int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } n long long cntleq(int idx, long long x) { n   return upper_bound(guy[idx].begin(), guy[idx].end(), x) - guy[idx].begin(); n } n long long choose2(long long a) { return a * (a - 1) / 2; } n long long choose3(long long a) { return a * (a - 1) * (a - 2) / 6; } n int32_t main() { n   ios_base::sync_with_stdio(0); n   srand(time(0)); n   if (fopen( cf1007b.in ,  r )) { n     freopen( cf1007b.in ,  r , stdin); n   } n   for (int i = 1; i < 100013; i++) { n     for (int j = i; j < 100013; j += i) { n       factors[j].push_back(i); n     } n   } n   cin >> Q; n   for (int q = 0; q < Q; q++) { n     cin >> X[0] >> X[1] >> X[2]; n     sort(X, X + 3); n     long long ans = 0; n     guy[1] = factors[X[0]]; n     guy[2] = factors[X[1]]; n     guy[3] = factors[gcd(X[0], X[1])]; n     guy[4] = factors[X[2]]; n     guy[5] = factors[gcd(X[0], X[2])]; n     guy[6] = factors[gcd(X[1], X[2])]; n     guy[7] = factors[gcd(X[0], gcd(X[1], X[2]))]; n     sz[7] = guy[7].size(); n     sz[6] = guy[6].size() - guy[7].size(); n     sz[5] = guy[5].size() - guy[7].size(); n     sz[3] = guy[3].size() - guy[7].size(); n     sz[4] = guy[4].size() - guy[6].size() - guy[5].size() + guy[7].size(); n     sz[2] = guy[2].size() - guy[6].size() - guy[3].size() + guy[7].size(); n     sz[1] = guy[1].size() - guy[3].size() - guy[5].size() + guy[7].size(); n     for (int i = 1; i < 8; i++) { n       pref[i] = guy[i].size(); n     } n     long long one = 0, two = 0, three = 0; n     one = sz[7]; n     for (int i = 1; i < 8; i++) { n       for (int j = 1; j < 8; j++) { n         if (!works(i, i, j)) { n           continue; n         } n         if (i == j) continue; n         two += sz[i] * sz[j]; n       } n     } n     two += choose2(sz[7]) * 2; n     for (int i = 1; i < 8; i++) { n       for (int j = i + 1; j < 8; j++) { n         for (int k = j + 1; k < 8; k++) { n           if (!works(i, j, k)) continue; n           three += sz[i] * sz[j] * sz[k]; n         } n       } n     } n     for (int i = 1; i < 8; i++) { n       for (int j = 1; j < 8; j++) { n         if (!works(i, i, j)) continue; n         if (i == j) continue; n         three += choose2(sz[i]) * sz[j]; n       } n     } n     three += choose3(sz[7]); n     ans = one + two + three; n     cout << ans <<   n ; n   } n   cerr <<  time elapsed =   << (clock() / (CLOCKS_PER_SEC / 1000)) <<   ms  n        << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 200000 + 10; n vector<int> G[maxn]; n vector<int> inv[maxn]; n int n, m; n int vis[2][maxn]; n int in[maxn]; n inline int topo_sort(vector<int>* G, int* in) { n   stack<int> S; n   for (auto i = (0); i < (n); ++i) { n     if (in[i] == 0) S.push(i); n   } n   int cnt = 0; n   for (; !S.empty();) { n     int u = S.top(); n     S.pop(); n     ++cnt; n     for (auto v : G[u]) { n       if (--in[v] == 0) S.push(v); n     } n   } n   return cnt == n; n } n inline void dfs(int u, vector<int>* g, int* vis) { n   if (vis[u]) return; n   vis[u] = 1; n   for (auto v : g[u]) { n     if (vis[v]) continue; n     dfs(v, g, vis); n   } n } n inline void solve() { n   int ans = 0; n   string o; n   for (auto i = (0); i < (n); ++i) { n     if (!vis[0][i] && !vis[1][i]) { n       o.push_back( A ); n       ans++; n     } else n       o.push_back( E ); n     dfs(i, G, vis[0]); n     dfs(i, inv, vis[1]); n   } n   printf( %d n%s n , ans, o.c_str()); n } n int main(void) { n   scanf( %d , &(n)), scanf( %d , &(m)); n   for (auto i = (0); i < (m); ++i) { n     int u, v; n     scanf( %d , &(u)), scanf( %d , &(v)); n     u--, v--; n     G[u].push_back(v); n     inv[v].push_back(u); n     in[v]++; n   } n   if (!topo_sort(G, in)) return puts( -1 ), 0; n   solve(); n   return 0; n } n