#include <bits/stdc++.h> n using namespace std; n int matriz[1010][1010]; n void verifica(int i, int j); n int main(void) { n   int n, m, d; n   d = 0; n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       char a; n       cin >> a; n       if (a ==  # ) { n         matriz[i][j] = 0; n         d++; n       } else n         matriz[i][j] = -1; n     } n   } n   for (int i = 1; i <= n - 2; i++) { n     for (int j = 1; j <= m - 2; j++) { n       verifica(i, j); n     } n   } n   int h = 0; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (matriz[i][j] > 0) { n         h++; n       } n     } n   } n   if (d == 0) { n     cout <<  YES  << endl; n   } else { n     if ((h == d) && (h != 0)) { n       cout <<  YES  << endl; n     } else { n       cout <<  NO  << endl; n     } n   } n } n void verifica(int a, int b) { n   int k = 1; n   if ((matriz[a - 1][b - 1] == -1) || (matriz[a - 1][b] == -1) || n       (matriz[a - 1][b + 1] == -1) || (matriz[a][b - 1] == -1) || n       (matriz[a][b + 1] == -1) || (matriz[a + 1][b - 1] == -1) || n       (matriz[a + 1][b] == -1) || (matriz[a + 1][b + 1] == -1)) { n     k = 0; n   } n   if (k) { n     matriz[a - 1][b - 1] += 1; n     matriz[a - 1][b] += 1; n     matriz[a - 1][b + 1] += 1; n     matriz[a][b - 1] += 1; n     matriz[a][b + 1] += 1; n     matriz[a + 1][b - 1] += 1; n     matriz[a + 1][b] += 1; n     matriz[a + 1][b + 1] += 1; n   } n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC optimization( unroll-loops ) n using namespace std; n const long long large = 1e11, remi2 = 998244353; n const long long remi = 1e9 + 7, inf = LLONG_MAX; n const long double Pi = acos(-1); n long long PowI(long long a, long long b, long long m) { n   long long ans = 1 % m; n   while (b > 0) { n     if (b % 2) ans = (((ans % m) * (a % m)) % m); n     a = (((a % m) * (a % m)) % m); n     b = (long long)(b / ((long long)2)); n   } n   return ans; n } n void Foxtrot() { n   long long n, k; n   cin >> n >> k; n   vector<long long> v(n); n   for (int i = 0; i < n; i++) cin >> v[i]; n   sort(v.begin(), v.end()); n   long long sum = 0; n   for (int i = 0; i < (n - (2 * k)); i++) { n     sum += v[i]; n   } n   for (long long i = n - 2 * k; i < (n - k); i++) { n     sum += (v[i] / v[i + k]); n   } n   cout << sum <<   n ; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long T = 1; n   cin >> T; n   for (int i = 1; i < T + 1; i++) { n     Foxtrot(); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(3) n using namespace std; n bool Finish_read; n template <class T> n inline void read(T &x) { n   Finish_read = 0; n   x = 0; n   int f = 1; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) f = -1; n     if (ch == EOF) return; n     ch = getchar(); n   } n   while (isdigit(ch)) x = x * 10 + ch -  0 , ch = getchar(); n   x *= f; n   Finish_read = 1; n } n template <class T> n inline void print(T x) { n   if (x / 10 != 0) print(x / 10); n   putchar(x % 10 +  0 ); n } n template <class T> n inline void writeln(T x) { n   if (x < 0) putchar( - ); n   x = abs(x); n   print(x); n   putchar(  n ); n } n template <class T> n inline void write(T x) { n   if (x < 0) putchar( - ); n   x = abs(x); n   print(x); n } n const int maxn = 100005, maxlog = 20; n int mx[maxlog][maxn], n, c, v[100005]; n long long f[maxn], sum[maxn]; n inline int get(int l, int r) { n   int k = log2(r - l + 1); n   return min(mx[k][l], mx[k][r - (1 << k) + 1]); n } n int main() { n   read(n); n   read(c); n   for (int i = 1; i <= n; i++) { n     read(v[i]); n     mx[0][i] = v[i]; n     sum[i] = sum[i - 1] + v[i]; n   } n   for (int j = 1; (1 << j) <= n; j++) n     for (int i = 1; i + (1 << j) - 1 <= n; i++) n       mx[j][i] = min(mx[j - 1][i], mx[j - 1][i + (1 << (j - 1))]); n   for (int i = 1; i < c; i++) f[i] = f[i - 1] + v[i]; n   for (int i = c; i <= n; i++) n     f[i] = min(f[i - 1] + v[i], n                f[i - c] + sum[i] - sum[i - c] - get(i - c + 1, i)); n   printf( %lld n , f[n]); n } n


#include <bits/stdc++.h> n using namespace std; n int i, j, n, t, k1[10000], k2[10000], cz1 = 0, cz11 = 0; n char s1[10000], s2[10000]; n int main() { n   scanf( %d , &n); n   while (n--) { n     scanf( %d , &t); n     scanf( %s , s1); n     scanf( %s , s2); n     for (i = 0; i < strlen(s1); i++) { n       k1[i] = s1[i] -  0 ; n       if (k1[i] == 0) { n         cz1++; n       } n     } n     for (i = 0; i < strlen(s2); i++) { n       k2[i] = s2[i] -  0 ; n       if (k2[i] == 0) { n         cz1++; n       } n     } n     if (cz1 == 0) { n       printf( YES n ); n     } else { n       for (j = 0; j < t; j++) { n         if (k1[j] != 0 && k2[j] != 0) { n           cz11++; n         } n       } n       if (cz11 >= 1) { n         printf( NO n ); n       } else { n         printf( YES n ); n       } n     } n     cz1 = 0, cz11 = 0; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long n, p; n int get(int x) { n   int sum = 0; n   while (x) { n     if (x & 1) sum++; n     x >>= 1; n   } n   return sum; n } n int main() { n   scanf( %lld%lld , &n, &p); n   int sum = 0; n   while (n - sum * p > 0) { n     int s = get(n - sum * p); n     if (sum >= s && sum <= n - sum * p) { n       printf( %d n , sum); n       return 0; n     } n     sum++; n   } n   puts( -1 ); n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   long long n; n   cin >> n; n   vector<long long> a(n), b(n), idxs(n + 1), ans(n, n); n   multiset<long long> s1, s2; n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n     idxs[a[i]] = i; n   } n   for (long long i = 0; i < n; i++) { n     cin >> b[i]; n     if (idxs[b[i]] < i) { n       s1.insert(i - idxs[b[i]]); n     } else { n       s2.insert(idxs[b[i]] - i); n     } n   } n   for (long long i = 0; i < n; i++) { n     while (s1.size() && *s1.begin() == i) { n       s2.insert(-i); n       s1.erase(s1.find(i)); n     } n     if (s1.size()) { n       ans[i] = min(ans[i], *s1.begin() - i); n     } n     if (s2.size()) { n       ans[i] = min(ans[i], i + *s2.begin()); n     } n     assert(s2.count(idxs[b[i]] - i)); n     s2.erase(s2.find(idxs[b[i]] - i)); n     s1.insert(n - idxs[b[i]] + i); n   } n   for (long long i = 0; i < n; i++) cout << ans[i] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long dp[300005]; n int main() { n   long long n, k; n   cin >> n >> k; n   for (int i = 1; i <= n; i++) n     for (int j = i + i; j <= n; j += i) dp[j]++; n   long long sum = 0, m = 0; n   for (int i = 1; i <= n; i++) { n     sum += dp[i]; n     if (sum >= k) { n       m = i; n       break; n     } n   } n   vector<pair<long long, long long> > vec; n   for (int i = m / 2 + 1; i <= m; i++) vec.emplace_back(dp[i], i); n   sort(vec.rbegin(), vec.rend()); n   set<int> st; n   for (int i = 1; i <= m; i++) st.insert(i); n   k = sum - k; n   for (int i = 0; i < vec.size() and k > 0; i++) { n     if (vec[i].first <= k) { n       k -= vec[i].first; n       st.erase(vec[i].second); n     } n   } n   if (k != 0) n     cout <<  No  << endl; n   else { n     cout <<  Yes  << endl; n     cout << st.size() << endl; n     for (int val : st) cout << val <<    ; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, i, j, k, s, p, x[100001], t[100001], w[100001]; n void sort1(int l, int r) { n   int i, j, y, z; n   i = l; n   j = r; n   y = x[(l + r) / 2]; n   do { n     while (x[i] < y) { n       i++; n     } n     while (x[j] > y) { n       j--; n     } n     if (!(i > j)) { n       z = x[i]; n       x[i] = x[j]; n       x[j] = z; n       z = t[i]; n       t[i] = t[j]; n       t[j] = z; n       i++; n       j--; n     } n   } while (!(i > j)); n   if (l < j) { n     sort1(l, j); n   } n   if (i < r) { n     sort1(i, r); n   } n } n void sort2(int l, int r) { n   int i, j, y, z; n   i = l; n   j = r; n   y = t[(l + r) / 2]; n   do { n     while (t[i] < y) { n       i++; n     } n     while (t[j] > y) { n       j--; n     } n     if (!(i > j)) { n       z = x[i]; n       x[i] = x[j]; n       x[j] = z; n       z = t[i]; n       t[i] = t[j]; n       t[j] = z; n       z = w[i]; n       w[i] = w[j]; n       w[j] = z; n       i++; n       j--; n     } n   } while (!(i > j)); n   if (l < j) { n     sort2(l, j); n   } n   if (i < r) { n     sort2(i, r); n   } n } n int main() { n   scanf( %d %d n , &n, &m); n   for (i = 1; i <= n; i++) { n     scanf( %d %d n , &t[i], &x[i]); n   } n   i = m; n   s = 0; n   while (i <= n) { n     s = max(s, t[i]); n     sort1(i - m + 1, i); n     p = x[i]; n     k = 1; n     w[i - m + 1] = s + x[i - m + 1]; n     for (j = i - m + 2; j <= i; j++) { n       if (x[j] == x[j - 1]) { n         k++; n       } else { n         s += 1 + (k / 2); n         k = 1; n       } n       w[j] = s + x[j]; n     } n     s += p + 1 + p + (k / 2); n     i += m; n   } n   if (i - m < n) { n     s = max(s, t[n]); n     sort1(i - m + 1, n); n     p = x[n]; n     k = 1; n     w[i - m + 1] = s + x[i - m + 1]; n     for (j = i - m + 2; j <= n; j++) { n       if (x[j] == x[j - 1]) { n         k++; n       } else { n         s += 1 + k / 2; n         k = 1; n       } n       w[j] = s + x[j]; n     } n     s += p + 1 + p + k / 2; n   } n   sort2(1, n); n   for (i = 1; i <= n; i++) { n     printf( %d  , w[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, k; n priority_queue<pair<long long, pair<long long, long long>>> pq; n long long divide(long long x, long long y) { n   if (x < y) return 1e16; n   long long t = x / y, rm = x % y; n   t += rm / y; n   rm %= y; n   return rm * (t + 1) * (t + 1) + (y - rm) * t * t; n } n void solve() { n   cin >> n >> k; n   long long ans = 0; n   k -= n; n   while (n--) { n     long long x; n     cin >> x; n     ans += x * x; n     pq.push({x * x - divide(x, 2), {x, 2}}); n   } n   while (k--) { n     auto x = pq.top(); n     pq.pop(); n     ans -= x.first; n     pq.push({divide(x.second.first, x.second.second) - n                  divide(x.second.first, x.second.second + 1), n              {x.second.first, x.second.second + 1}}); n   } n   cout << ans <<   n ; n } n signed main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   solve(); n } n


#include<bits/stdc++.h> n using namespace std; n #define ll long long n #define ull unsigned long long n #define inf INT_MAX n #define minf INT_MIN n #define infl LLONG_MAX n #define minfl LLONG_MIN n #define mod 1000000007 n  n  n /*----------------------Debugger-----------------------------------*/ n  n #ifndef ONLINE_JUDGE n #define show(x) cerr<< #x << :  ; _print(x); cerr <<endl; n #else n #define show(x) n #endif n  n template<class T> void _print(T x){ cerr<<x<<endl; } n template<class T> void _print(vector<T> x){cerr<< [ ; for(auto i: x) cerr<<i<< ,  ; cerr<< ] n ;} n template<class T> void _print(set<T> x){ cerr<< {  ; for(auto i: x) cerr<<i<< ,  ; cerr<< } n ;} n  n  n /*-------------------------------------------------------------------------------------------------*/ n  n ll exp(int a,int b,int p){ll res=1; a=a%p;  n  while(b){ if(b & 1) res=(res*a)%p; a=((ll)a*a)%p; b=b>>1; } return res; } n  n ll mmiPrime(int a, int p){ return exp(a,p-2,p); } n  n pair<int,int> extendedGCD(int a, int b){ if(b==0){ pair<int,int> ans; ans.first=1; ans.second=0; return ans; } n  pair<int,int> temp=extendedGCD(b,a%b); pair<int,int> ans; n  ans.first=temp.second; ans.second=temp.first - (a/b)*temp.second; return ans; } n  n int mmi(int a, int m){ pair<int,int> tmp=extendedGCD(a,m); return tmp.first; } n  n int ncr(int n,int r, int p, int fact[]){ return ((fact[n] * mmiPrime(fact[r],p) % p) * mmiPrime(fact[n-r],p))%p; } n  n /*-------------------------------------------------------------------------------------------------*/ n  n  n void solve(){ n  int n,k; n  cin>>n>>k; n  int a[k]; n  int t[k]; n  n  for(int i=0;i<k;i++) n   cin>>a[i]; n  n  n  for(int i=0;i<k;i++) n   cin>>t[i]; n  n  vector<ll> ac(n+1,INT_MAX); n  n  for(int i=0;i<k;i++){ n   ac[a[i]]=t[i]; n  } n  n  n  ll L[n+1],R[n+1]; n  n  ll p=INT_MAX; n  for(int i=1;i<=n;i++){ n   p=min(p+1,ac[i]); n   L[i]=p; n  } n  n  p=INT_MAX; n  for(int i=n;i>0;i--){ n   p=min(p+1,ac[i]); n   R[i]=p; n  } n  n  for(int i=1;i<=n;i++) n   cout<<min(L[i],R[i])<<   ; n  cout<<  n ; n  n } n  n int main(){ n  ios_base::sync_with_stdio(false); cin.tie(NULL); n  n  #ifndef ONLINE_JUDGE n   freopen( input.txt , r ,stdin); n   freopen( output.txt , w ,stdout); n   freopen( error.txt , w ,stderr); n  #endif n  n  clock_t time = clock(); n  n  int t; n  cin>>t; n  while(t--){ n   solve(); n  } n  n  time = clock() - time; n   n  #ifndef ONLINE_JUDGE n   cout<< time:  <<time*1000/CLOCKS_PER_SEC<<  miliseconds n ; n  #endif n  n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n int gcd(int a, int b) { n   if (a == 0) return b; n   return gcd(b % a, a); n } n long long int power(long long int x, long long int y, long long int p) { n   long long int res = 1; n   x = x % p; n   while (y > 0) { n     if (y & 1) res = (res * x) % p; n     y = y >> 1; n     x = (x * x) % p; n   } n   return res; n } n int main() { n   int n; n   cin >> n; n   long long int n1 = 0, n0 = 0, x1 = 0, x0 = 0; n   string a, b; n   cin >> a >> b; n   for (int i = 0; i < n; i++) { n     if (a[i] ==  0  && b[i] ==  1 ) n0++; n     if (a[i] ==  1  && b[i] ==  1 ) n1++; n     if (a[i] ==  1  && b[i] ==  0 ) x1++; n     if (a[i] ==  0  && b[i] ==  0 ) x0++; n   } n   long long int ans = 0; n   for (int i = 0; i < n; i++) { n     if (b[i] ==  0 ) { n       if (a[i] ==  1 ) n         ans += n0; n       else n         ans += n1; n     } n   } n   ans += x1 * x0; n   cout << ans; n   return 0; n } n


/*************************************************************** n  File name: CF1517F.cpp n  Author: ljfcnyali n  Create time: 2021年04月27日 星期二 10时19分32秒 n ***************************************************************/ n #include<bits/stdc++.h> n  n using namespace std; n  n #define REP(i, a, b) for ( int i = (a), _end_ = (b); i <= _end_; ++ i )  n #define mem(a) memset ( (a), 0, sizeof ( a ) )  n #define str(a) strlen ( a )  n #define pii pair<int, int> n #define int long long n typedef long long LL; n  n const int maxn = 1010; n const int Mod = 998244353; n  n int n, Begin[maxn], Next[maxn], To[maxn], e, ans[maxn], ret, f[maxn][maxn][2], g[maxn][2], Max[maxn]; n  n inline void add(int u, int v) { To[++ e] = v; Next[e] = Begin[u]; Begin[u] = e; }  n  n inline int power(int a, int b) { int r = 1; while ( b ) { if ( b & 1 ) r = r * a % Mod; a = a * a % Mod; b >>= 1; } return r; } n  n inline void DFS(int u, int fa, int x) n { n  f[u][0][0] = f[u][x][1] = 1; Max[u] = 0; n  for ( int i = Begin[u]; i; i = Next[i] )  n  { n   int v = To[i]; if ( v == fa ) continue ;  n   DFS(v, u, x);  n   REP(k, 0, Max[u]) REP(j, 0, Max[v]) n   { n    g[max(k, j + 1)][0] = (g[max(k, j + 1)][0] + f[u][k][0] * f[v][j][0]) % Mod; n    if ( j < x - k ) g[x - k][1] = (g[x - k][1] + f[u][x - k][1] * f[v][j][0]) % Mod; n    else g[j + 1][0] = (g[j + 1][0] + f[u][x - k][1] * f[v][j][0]) % Mod; n   } n  n   REP(k, 0, Max[u]) REP(j, x - Max[v], x) n   { n    if ( k <= j - 1 ) g[j - 1][1] = (g[j - 1][1] + f[u][k][0] * f[v][j][1]) % Mod; n    else g[k][0] = (g[k][0] + f[u][k][0] * f[v][j][1]) % Mod; n    g[max(x - k, j - 1)][1] = (g[max(x - k, j - 1)][1] + f[u][x - k][1] * f[v][j][1]) % Mod; n   } n   REP(k, 0, x) REP(o, 0, 1) { f[u][k][o] = g[k][o]; g[k][o] = 0; }  n   Max[u] = max(Max[u], Max[v] + 1); n  } n } n  n inline int Solve(int x) n { n  mem(f); DFS(1, 0, x); n  int sum = 0; REP(i, 0, x) sum = (sum + f[1][i][1]) % Mod; return sum; n } n  n signed main() n { n #ifndef ONLINE_JUDGE n     freopen( input.txt ,  r , stdin); n     freopen( output.txt ,  w , stdout); n #endif n  scanf( %lld , &n); n  REP(i, 1, n - 1) { int u, v; scanf( %lld%lld , &u, &v); add(u, v); add(v, u); } n  REP(i, 0, n) ans[i] = Solve(i);   n  REP(i, 1, n) ret = (ret + (ans[i] - ans[i - 1]) * (i - 1)) % Mod; n  ret = (ret + n - 1) * power((Mod + 1) / 2, n) % Mod; n  printf( %lld n , (ret + Mod) % Mod); 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(); n   cout.tie(); n   int n, q, i; n   cin >> n; n   int arr[106] = {}; n   for (i = 1; i <= n; i++) { n     cin >> arr[i]; n   } n   cin >> q; n   int x, y; n   while (q--) { n     cin >> x >> y; n     arr[x - 1] += (y - 1); n     arr[x + 1] += (arr[x] - y); n     arr[x] = 0; n   } n   for (i = 1; i <= n; i++) { n     cout << arr[i] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct bus { n   double t, v; n   double cost, time; n }; n bus bus[100000]; n int main() { n   int n, a, d; n   scanf( %d%d%d , &n, &a, &d); n   double fastest = 0.0; n   double x, y; n   for (int i = 0; i < n; i++) { n     scanf( %lf%lf , &bus[i].t, &bus[i].v); n     x = (d - bus[i].v * bus[i].v / (2.0 * a)) / bus[i].v; n     y = bus[i].t + (bus[i].v / a) + x; n     if (x < 0) { n       y = bus[i].t + sqrt(2.0 * d / (double)a); n     } n     if (fastest <= y) fastest = y; n     printf( %.10f n , fastest); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100050; n vector<int> E[N]; n queue<int> q[2]; n int c, in[N], col[N], sol; n int main() { n   int n, m, u, v, i; n   scanf( %i %i , &n, &m); n   for (i = 0; i < n; i++) scanf( %i , &col[i]); n   while (m--) { n     scanf( %i %i , &u, &v); n     E[v].push_back(u); n     in[u]++; n   } n   for (i = 0; i < n; i++) n     if (!in[i]) q[col[i]].push(i); n   while (!q[0].empty() || !q[1].empty()) { n     if (q[c].empty()) { n       if (!c) sol++; n       c ^= 1; n     } n     u = q[c].front(); n     q[c].pop(); n     for (i = 0; i < E[u].size(); i++) { n       v = E[u][i]; n       in[v]--; n       if (!in[v]) q[col[v]].push(v); n     } n   } n   printf( %i n , sol); n   return 0; n } n


#include <bits/stdc++.h> n const int mod = 1e9 + 7; n using namespace std; n bool sortcol(const vector<int>& v1, const vector<int>& v2) { n   return v1[1] < v2[1]; n } n void fastio() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n bool compare(const pair<long long int, long long int>& a, n              const pair<long long int, long long int>& b) { n   if (a.first > b.first) return true; n   if (a.first == b.first) return a.second < b.second; n   return false; n } n long long int power(long long int x, long long int y) { n   long long int res = 1; n   x = x % mod; n   while (y > 0) { n     if (y & 1) res = (res * x) % mod; n     y = y >> 1; n     x = (x * x) % mod; n   } n   return res; n } n vector<int> vin(int n) { n   vector<int> v; n   for (int i = 0; i < n; i++) { n     int t; n     cin >> t; n     v.push_back(t); n   } n   return v; n } n vector<int> ch; n void solve() { n   long long int n, k; n   cin >> n >> k; n   long long int a[n]; n   for (int i = 0; i < n; i++) cin >> a[i]; n   long long int l = a[0] - k, r = a[0] + k; n   for (int i = 0; i < n; i++) { n     long long int x = a[i] - k; n     long long int y = a[i] + k; n     l = max(l, x); n     r = min(r, y); n   } n   if (l > r) { n     cout << -1 <<   n ; n   } else n     cout << r <<   n ; n } n int main() { n   int tc; n   cin >> tc; n   while (tc--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n long long qpow(long long a, long long b) { n   long long ans = 1; n   while (b) { n     if (b % 2) ans = ans * a; n     a = a * a; n     b = b / 2; n   } n   return ans; n } n vector<int> pos; n stack<long long> num; n stack<char> ch; n long long cal(string s) { n   while (!num.empty()) num.pop(); n   while (!ch.empty()) ch.pop(); n   for (int i = 0; i < s.length(); i++) { n     char c = s[i]; n     if (c >=  0  && c <=  9 ) n       num.push(c -  0 ); n     else if (c ==  ( ) { n       ch.push(c); n     } else if (c ==  ) ) { n       while (ch.top() !=  ( ) { n         char x = ch.top(); n         ch.pop(); n         long long a = num.top(); n         num.pop(); n         long long b = num.top(); n         num.pop(); n         if (x ==  * ) n           num.push(a * b); n         else n           num.push(a + b); n       } n       ch.pop(); n     } else { n       if (c ==  + ) { n         while (!ch.empty() && ch.top() ==  * ) { n           char x = ch.top(); n           ch.pop(); n           long long a = num.top(); n           num.pop(); n           long long b = num.top(); n           num.pop(); n           if (x ==  * ) n             num.push(a * b); n           else n             num.push(a + b); n         } n         ch.push(c); n       } else n         ch.push(c); n     } n   } n   while (!ch.empty()) { n     char x = ch.top(); n     ch.pop(); n     long long a = num.top(); n     num.pop(); n     long long b = num.top(); n     num.pop(); n     if (x ==  * ) n       num.push(a * b); n     else n       num.push(a + b); n   } n   return num.top(); n } n int main() { n   ios::sync_with_stdio(false); n   string str; n   while (cin >> str) { n     int n = str.size(); n     pos.clear(); n     pos.push_back(-1); n     for (int i = 1; i < n; i += 2) { n       if (str[i] ==  * ) { n         pos.push_back(i); n       } n     } n     pos.push_back(n); n     int len = pos.size(); n     long long ans = 0; n     for (int i = 0; i < len - 1; i++) { n       for (int j = i + 1; j < len; j++) { n         string s = str; n         s.insert(pos[i] + 1, 1,  ( ); n         s.insert(pos[j] + 1, 1,  ) ); n         ans = max(ans, cal(s)); n       } n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e4 + 7; n bitset<N> a[60]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0), cout.tie(0); n   int n, m; n   cin >> m >> n; n   for (int i = 0, t; i < m; i++) { n     cin >> t; n     for (int j = 0, x; j < t; j++) cin >> x, a[i].set(x); n     for (int j = 0; j < i; j++) { n       if ((a[i] & (~a[j])) == a[i]) return cout <<  impossible  << endl, 0; n     } n   } n   cout <<  possible n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> build(int n) { n   map<int, bool> s; n   for (int i = 2; i * i <= n; i++) { n     if (s[i] == false) { n       for (int j = 2 * i; j <= n; j = j + i) s[j] = true; n     } n   } n   vector<int> t; n   for (int i = 2; i <= n; i++) { n     if (s[i] == false) t.push_back(i); n   } n   return t; n } n int main() { n   int n; n   cin >> n; n   vector<int> a = build(n); n   int b[n - 1]; n   int s = 1; n   for (int i = 0; i < a.size(); i++) { n     for (int j = a[i]; j <= n; j = j + a[i]) b[j - 2] = s; n     s++; n   } n   for (int i = 0; i < n - 1; i++) cout << b[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long sum, i, j; n   cin >> sum; n   vector<long> length(3); n   for (i = 0; i < 3; i++) cin >> length[i]; n   vector<vector<long> > dp(2, vector<long>(sum + 1, INT_MIN)); n   dp[3 % 2][0] = 0; n   for (i = 2; i >= 0; i--) { n     for (j = 0; j <= sum; j++) { n       dp[i % 2][j] = dp[(i + 1) % 2][j]; n       if (j - length[i] >= 0 && dp[i % 2][j - length[i]] != INT_MIN) n         dp[i % 2][j] = max(dp[i % 2][j], 1 + dp[i % 2][j - length[i]]); n     } n   } n   cout << dp[0][sum]; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n long long add(long long x, long long y) { n   x += y; n   if (x >= mod) x -= mod; n   return x; n } n long long mul(long long x, long long y) { return (x * y) % mod; } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   long long x, s; n   cin >> s >> x; n   long long ret = 0; n   if (s == x) ret = -2; n   long long ans = 1; n   for (int i = 0; i <= 40; i++) { n     if (x & (1LL << (i))) { n       ans *= 2; n       if (s < (1LL << (i))) { n         cout << 0 << endl; n         exit(0); n       }; n       s -= (1LL << (i)); n     } n   } n   for (int i = 0; i <= 40; i++) { n     if (!(x & (1LL << (i)))) { n       if (s & (1LL << (i + 1))) { n         s -= (1LL << (i + 1)); n       } n     } n   } n   if (s != 0) { n     cout << 0 << endl; n     exit(0); n   }; n   cout << ans + ret << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n uint64_t rnd_data = 0xDEADBEEFDULL; n inline void my_srand(int seed) { rnd_data = ((uint64_t)seed << 16) | 0x330E; } n inline int my_rand() { n   rnd_data = rnd_data * 0x5DEECE66DULL + 0xB; n   return (rnd_data >> 17) & 0x7FFFFFFF; n } n template <typename T> n void my_random_shuffle(T b, T e) { n   for (int i = (1); i <= (int)((int)(e - b) - 1); i++) { n     swap(b[i], b[my_rand() % (i + 1)]); n   } n } n template <class _T> n inline _T sqr(const _T &x) { n   return x * x; n } n template <typename type_t, typename less_t = std::less<type_t>> n inline bool uin(type_t &a, const type_t &b, const less_t &ls = less_t()) { n   return ls(b, a) ? a = b, true : false; n } n template <typename type_t, typename less_t = std::less<type_t>> n inline bool uax(type_t &a, const type_t &b, const less_t &ls = less_t()) { n   return ls(a, b) ? a = b, true : false; n } n const long double PI = 3.1415926535897932384626433832795L; n const long double EPS = 1e-9; n const int MAXN = 202400; n const size_t LIM = 300; n int n; n long double p[MAXN]; n vector<int> a[MAXN]; n vector<int> al[MAXN]; n vector<int> ah[MAXN]; n long double s[MAXN]; n long double sh[MAXN]; n bool hv[MAXN]; n void precalc() { n   for (int i = 0; i < (int)(n); i++) { n     s[i] = 0.0; n     for (int vn : al[i]) { n       s[i] += p[vn]; n     } n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < (int)(n); i++) { n     double x; n     scanf( %lf , &x); n     p[i] = x; n   } n   for (int i = 0; i < (int)(n - 1); i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     a[x].push_back(y); n     a[y].push_back(x); n   } n   for (int i = 0; i < (int)(n); i++) { n     hv[i] = a[i].size() > LIM; n     sh[i] = 0.0; n   } n   for (int i = 0; i < (int)(n); i++) { n     for (int vn : a[i]) { n       if (hv[vn]) { n         ah[i].push_back(vn); n       } else { n         al[i].push_back(vn); n       } n     } n   } n   precalc(); n   for (int i = 0; i < (int)(n); i++) { n     if (hv[i]) { n       for (auto vn : a[i]) { n         sh[i] += 1.0 - p[vn]; n       } n     } n   } n   int m; n   scanf( %d , &m); n   long double spall = 0.0; n   for (int i = 0; i < (int)(n); i++) { n     spall += p[i]; n   } n   long double sumprev = 0.0; n   for (int j = 0; j < (int)(n); j++) { n     sumprev += a[j].size() * p[j]; n     sumprev += (1.0 - p[j]) * s[j]; n     sumprev += p[j] * sh[j]; n   } n   for (int i = 0; i < (int)(m); i++) { n     int x; n     double y; n     scanf( %d%lf , &x, &y); n     spall -= p[x]; n     sumprev -= a[x].size() * p[x] + (1.0 - p[x]) * s[x] + p[x] * sh[x]; n     if (!hv[x]) { n       for (int vn : a[x]) { n         sumprev -= (1.0 - p[vn]) * s[vn]; n         s[vn] -= p[x]; n       } n     } n     for (auto vn : ah[x]) { n       sumprev -= p[vn] * sh[vn]; n       sh[vn] -= 1.0 - p[x]; n     } n     p[x] = y; n     for (auto vn : ah[x]) { n       sh[vn] += 1.0 - p[x]; n       sumprev += p[vn] * sh[vn]; n     } n     if (!hv[x]) { n       for (int vn : a[x]) { n         s[vn] += p[x]; n         sumprev += (1.0 - p[vn]) * s[vn]; n       } n     } n     spall += p[x]; n     sumprev += a[x].size() * p[x] + (1.0 - p[x]) * s[x] + p[x] * sh[x]; n     long double ans = 2.0 + sumprev; n     ans -= spall * 2.0; n     ans /= 2.0; n     printf( %0.9f n , (double)ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, x, ans = 0; n vector<int> a, b; n bool cmp(const int &a, const int &b) { return a > b; } n int main(int argc, char *argv[]) { n   scanf( %d%d , &n, &x); n   a.resize(n); n   b.resize(n); n   for (int i = 0; i < n; ++i) { n     scanf( %d , &a[i]); n   } n   for (int i = 0; i < n; ++i) { n     scanf( %d , &b[i]); n   } n   sort(a.begin(), a.end(), cmp); n   sort(b.begin(), b.end(), cmp); n   for (int i = 0, j = n - 1; i < n && j >= 0; ++i) { n     for (; j >= 0 && a[i] + b[j] < x; --j) n       ; n     if (j >= 0) { n       ++ans; n     } n     --j; n   } n   printf( 1 %d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 30; n int n, m, T; n string s[N], t[N]; n int main() { n   ios::sync_with_stdio(false), cin.tie(0); n   cin >> n >> m; n   for (int i = 0; i < n; i++) cin >> s[i]; n   for (int i = 0; i < m; i++) cin >> t[i]; n   cin >> T; n   while (T--) { n     int x; n     cin >> x; n     x--; n     cout << s[x % n] << t[x % m] <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct Hungary { n   vector<int> g[1005]; n   int from[1005], mx[1005], ans; n   bool use[1005]; n   int n, sume; n   int nume[1005]; n   bool match(int x, int exp) { n     for (int i = int(0); i < int(g[x].size()); ++i) { n       if (g[x][i] == exp) continue; n       if (!use[g[x][i]]) { n         use[g[x][i]] = true; n         if (from[g[x][i]] == -1 || match(from[g[x][i]], exp)) { n           from[g[x][i]] = x; n           mx[x] = g[x][i]; n           return true; n         } n       } n     } n     return false; n   } n   void matching(int exp) { n     ans = 0; n     memset(from, -1, sizeof(from)); n     memset(mx, 0, sizeof(mx)); n     for (int i = int(1); i < int(n + 1); ++i) { n       if (i == exp) continue; n       memset(use, 0, sizeof(use)); n       if (match(i, exp)) ans++; n     } n   } n   void init(int nn) { n     n = nn; n     for (int i = int(1); i < int(n + 1); ++i) g[i].clear(); n     sume = 0; n     memset(nume, 0, sizeof(nume)); n   } n   void AddEdge(int u, int v) { n     g[u].push_back(v); n     sume++; n     nume[u]++; n     if (u != v) nume[v]++; n   } n } H; n bool mp[1005][1005]; n int main() { n   int n, m; n   while (scanf( %d%d , &n, &m) == 2) { n     memset(mp, 0, sizeof(mp)); n     H.init(n); n     for (int i = int(0); i < int(m); ++i) { n       int u, v; n       scanf( %d%d , &u, &v); n       mp[u][v] = true; n       H.AddEdge(u, v); n     } n     int ans = n * n * n; n     for (int i = int(1); i < int(n + 1); ++i) { n       int pans = 0; n       if (!mp[i][i]) pans++; n       for (int j = int(1); j < int(n + 1); ++j) { n         if (j == i) continue; n         if (!mp[i][j]) pans++; n         if (!mp[j][i]) pans++; n       } n       H.matching(i); n       pans += (H.sume - H.nume[i] - H.ans); n       pans += (n - 1 - H.ans); n       ans = min(ans, pans); n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e9 + 7; n const int N = 1e6 + 10; n int main() { n   int n; n   cin >> n; n   map<int, int> mc; n   for (int i = 1; i <= n; i++) { n     int x; n     cin >> x; n     mc[x] = i; n   } n   pair<int, int> ans = {INF, INF}; n   for (auto &p : mc) { n     pair<int, int> pp = p; n     swap(pp.first, pp.second); n     ans = min(ans, pp); n   } n   cout << ans.second << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long const INF = 1e18; n long long s[300100], a[300100]; n int n, k; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> n >> k; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   s[n + 1] = 0; n   for (int i = n; i >= 1; i--) s[i] = s[i + 1] + a[i]; n   s[0] = s[1]; n   sort(s + 2, s + n + 1); n   long long res = 0; n   for (int i = n; i >= n - k + 2; i--) res = res + s[i]; n   res += s[1]; n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double tux, pur, foo, bar, baz, quz = 1; n int main() { n   cin >> tux; n   for (int i = 1; i <= tux; i++) { n     cin >> pur; n     foo += pur; n     bar++; n     if (foo * quz > baz * bar) { n       baz = foo; n       quz = bar; n     } n   } n   printf( %.6lf , baz / quz); n   return 0; n } n


#include <bits/stdc++.h> n int main(int argc, char **argv) { n   long count = 0; n   std::cin >> count; n   long sum = 0; n   long tmp; n   long size; n   do { n     sum += (tmp = count); n     size = (count / 2) + 1; n     int offset = (count % 2) ? 2 : 1; n     int start = (count % 2) ? 3 : 2; n     for (long i = start; i < size; i += offset) { n       if (!(count % i)) { n         tmp = i; n         break; n       } n     } n     count /= tmp; n   } while (count != 1); n   std::cout << sum + 1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, men = 0, pluss = 0, nol = 0; n long long a, ans = 0; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a; n     if (a < 0) { n       ans += (-1 - a); n       men++; n     } else if (a > 0) { n       ans += (a - 1); n       pluss++; n     } else { n       nol++; n     } n   } n   if (nol > 0) { n     cout << ans + nol << endl; n   } else { n     if (men % 2 == 0) { n       cout << ans << endl; n     } else { n       cout << ans + 2 << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long a, b, c, d, e, f; n   cin >> a >> b >> c >> d >> e >> f; n   if (d == 0) n     cout <<  Hermione  << endl; n   else if (c == 0) n     cout <<  Ron  << endl; n   else if (b == 0) n     cout <<  Hermione  << endl; n   else if (a == 0) n     cout <<  Ron  << endl; n   else if (f == 0) n     cout <<  Hermione  << endl; n   else if (e == 0) n     cout <<  Ron  << endl; n   else n     cout << (b * d * f > a * c * e ?  Ron  :  Hermione ) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ul = unsigned long long; n using ld = long double; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   ll n; n   cin >> n; n   vector<ll> a(n); n   vector<pair<ll, ll>> b; n   for (int i = 0; i < (n); i++) { n     cin >> a[i]; n     a[i] -= i; n     while (1) { n       if (b.size() > 1 && n           (b[b.size() - 2].first - b.back().first) * b.back().second + n                   b[b.size() - 2].first <= n               a[i]) { n         b[b.size() - 2].second += b.back().second; n         a[i] -= (b[b.size() - 2].first - b.back().first) * b.back().second; n         b.pop_back(); n       } else if (!b.empty() && b.back().first <= a[i]) { n         ll d = (a[i] - b.back().first) / (b.back().second + 1); n         b.back().first += d; n         a[i] -= d * b.back().second; n         ll q = a[i] - b.back().first; n         if (q > 0) { n           pair<ll, ll> pp{b.back().first, b.back().second - q + 1}; n           b.back().first++; n           b.back().second = q; n           if (b.size() > 1 && b[b.size() - 2].first == b.back().first) { n             b[b.size() - 2].second += b.back().second; n             b.pop_back(); n           } n           b.push_back(pp); n         } else n           b.back().second++; n         break; n       } else { n         b.push_back({a[i], 1}); n         break; n       } n     } n   } n   ll cx = 0; n   for (auto &pp : b) n     for (int i = 0; i < (pp.second); i++) cout << cx++ + pp.first <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 2e3 + 5; n string s, p; n int nxt[MAX]; n vector<int> cost; n int memo[MAX][MAX]; n int roll(int idx, int val) { n   if (idx >= (int)cost.size()) { n     return 0; n   } n   int &ans = memo[idx][val]; n   if (~ans) { n     return ans; n   } n   ans = roll(idx + 1, val); n   if (val >= (int)cost[idx]) { n     ans = max(ans, roll(nxt[idx], val - cost[idx]) + 1); n   } n   return ans; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin >> s >> p; n   vector<pair<int, int> > ranges; n   for (int i = int(0); i < int(s.size()); i++) { n     int idx = 0; n     int match = -1; n     if (s[i] == p[0]) { n       for (int j = int(i); j < int(s.size()); j++) { n         if (p[idx] == s[j]) { n           idx++; n         } n         if (idx == (int)p.size()) { n           match = j; n           break; n         } n       } n     } n     if (match != -1) { n       ranges.emplace_back(i, match); n     } n   } n   for (int i = int(0); i < int(ranges.size()); i++) { n     auto it = lower_bound(ranges.begin(), ranges.end(), n                           make_pair(ranges[i].second + 1, 0)); n     nxt[i] = it - ranges.begin(); n     cost.push_back( n         max(0, ranges[i].second - ranges[i].first - (int)p.size() + 1)); n   } n   memset(memo, -1, sizeof memo); n   for (int i = int(0); i < int(s.size() + 1); i++) { n     cout << min(roll(0, i), ((int)s.size() - i) / (int)p.size()) <<    ; n   } n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 9; n vector<int> g[N]; n int a[N], cnt[N], b[N << 1], c[N << 1]; n int *vis = b + N, *val = c + N; n set<int> S, now; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = (1); i < (n + 1); ++i) n     scanf( %d , &a[i]), cnt[a[i]]++, g[a[i]].push_back(i); n   int maxn = 0, p = 0; n   for (int i = (1); i < (n + 1); ++i) { n     if (maxn < cnt[i]) { n       maxn = cnt[i]; n       p = i; n     } n   } n   if (maxn == n) { n     puts( 0 ); n     return 0; n   } n   int ans = 0; n   for (auto v : g[p]) S.insert(v); n   vector<int> temp; n   int key = 0; n   for (int i = (1); i < (n + 1); ++i) { n     if (p == i) continue; n     now.clear(); n     temp.clear(); n     for (auto v : g[i]) { n       auto it = S.lower_bound(v); n       if (it != S.end()) { n         now.insert(*it); n         S.erase(it); n       } n       it = S.lower_bound(v); n       if (it != S.end()) { n         now.insert(*it); n         S.erase(it); n       } n       it = S.lower_bound(v); n       if (it != S.begin()) { n         --it; n         now.insert(*it); n         S.erase(it); n       } n       it = S.lower_bound(v); n       if (it != S.begin()) { n         --it; n         now.insert(*it); n         S.erase(it); n       } n     } n     for (auto v : now) temp.push_back(v); n     for (auto v : g[i]) now.insert(v); n     int pos = -1, sum = 0; n     for (auto v : now) { n       if (v > pos) ++key; n       if (vis[sum] != key) { n         vis[sum] = key; n         val[sum] = v; n       } n       if (a[v] == p) ++sum; n       if (a[v] == i) --sum; n       auto it = S.lower_bound(v); n       if (it == S.end()) n         pos = n + 1; n       else n         pos = *it; n       if (vis[sum] == key) { n         int l, r; n         int vv = val[sum]; n         auto it = now.lower_bound(vv); n         if (it == now.begin()) n           l = 0; n         else n           l = *prev(it); n         it = now.lower_bound(v); n         ++it; n         if (it == now.end()) n           r = n + 1; n         else n           r = *it; n         ans = max(r - l - 1, ans); n       } n     } n     for (auto v : temp) S.insert(v); n   } n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = int(5e3); n const int MOD = int(1e9 + 7); n const int oo = INT_MAX; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n, d, h; n   while (cin >> n >> d >> h) { n     if (2 * h < d) { n       cout << -1 <<   n ; n       continue; n     } n     vector<pair<int, int> > edges; n     for (int i = 2; i <= h + 1; i++) { n       if (i > n) continue; n       edges.push_back({i - 1, i}); n     } n     for (int i = h + 2; i <= d + 1; i++) { n       if (i > n) continue; n       if (i == h + 2) n         edges.push_back({1, i}); n       else n         edges.push_back({i - 1, i}); n     } n     int id = d + 2, node = -1; n     if (h < d) n       node = 1; n     else if (h == d && h != 1) n       node = 2; n     if (node != -1) { n       while (id <= n) { n         edges.push_back({node, id}); n         id++; n       } n     } n     if (int(edges.size()) == n - 1) n       for (auto& e : edges) cout << e.first <<     << e.second <<   n ; n     else n       cout << -1 <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int n = 0, f = 1; n   char c; n   for (c = getchar(); c !=  -  && (c <  0  || c >  9 ); c = getchar()) n     ; n   if (c ==  - ) c = getchar(), f = -1; n   for (; c >=  0  && c <=  9 ; c = getchar()) n = n * 10 + c - 48; n   return n * f; n } n struct ar { n   int x, y, id, d; n } now; n bool operator<(ar a, ar b) { return a.x == b.x ? a.y < b.y : a.x < b.x; } n int i, a, b, d[3005]; n vector<ar> p, an; n bool cmp(ar a, ar b) { return a.x * b.y > a.y * b.x; } n void solve(vector<ar> p) { n   int i, j, k, D, n = p.size() - 1; n   if (!n) return; n   for (i = 1; i <= n; i++) n     if (p[i] < p[0]) swap(p[i], p[0]); n   for (i = 1; i <= n; i++) p[i].x -= p[0].x, p[i].y -= p[0].y; n   sort(p.begin() + 1, p.end(), cmp); n   for (i = 1; i <= n; i++) p[i].x += p[0].x, p[i].y += p[0].y; n   if (p[0].d < 0) { n     for (i = 1; i <= n; i++) { n       j = i, D = 0; n       for (;; i++) { n         D += p[i].d; n         if (D >= 0) break; n       } n       p[i].d--, an.push_back((ar){p[i].id, p[0].id}); n       p[i].d -= D; n       solve(vector<ar>(p.begin() + j, p.begin() + i + 1)); n       p[j = i].d = D - 1; n       for (; D;) D += p[++i].d; n       solve(vector<ar>(p.begin() + j, p.begin() + i + 1)); n     } n     return; n   } n   for (i = 1; p[0].d >= 0;) { n     for (j = i, D = 1; D; i++) D += p[i].d; n     p[0].d--, an.push_back((ar){p[0].id, p[i - 1].id}); n     solve(vector<ar>(p.begin() + j, p.begin() + i)); n   } n   solve(vector<ar>(p.begin() + i - 1, p.end())); n } n void work() { n   a = read(), b = read(); n   for (i = 1; i <= b; i++) d[i] = read(); n   p.clear(), an.clear(); n   for (i = 1; i <= a + b; i++) { n     now.x = read(), now.y = read(); n     if (i <= a) n       now.id = i, now.d = -1; n     else n       now.d = d[now.id = i - a] - 1; n     p.push_back(now); n   } n   solve(p); n   puts( YES ); n   for (i = 0; i <= a + b - 2; i++) printf( %d %d n , an[i].x, an[i].y); n } n int main() { n   for (int t = read(); t--;) work(); n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1e9; n void query() { n   int n, m; n   cin >> n >> m; n   vector<string> t(n); n   for (auto& i : t) cin >> i; n   vector<vector<int>> col(n, vector<int>(m, 0)); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) n       col[i][j] = (i > 0 ? col[i - 1][j] : 0) + t[i][j] -  0 ; n   int res = inf; n   queue<pair<int, int>> q; n   for (int a = 5; a <= n; a++) { n     for (int i = 0; i <= n - a; i++) { n       while (!q.empty()) q.pop(); n       pair<int, int> X(inf, 0); n       int add = 0; n       for (int j = 0; j < m; j++) { n         while (q.size() >= 3) { n           if (q.front().first - q.front().second < X.first - X.second) n             X = q.front(); n           q.pop(); n         } n         int A = col[i + a - 2][j] - col[i][j]; n         if (j - 3 >= 0) res = min(res, X.first - X.second + add + a - 2 - A); n         if (j > 0) n           add += A + 2 - (int)(t[i][j] -  0 ) - (int)(t[i + a - 1][j] -  0 ); n         q.push({a - 2 - A, add}); n       } n     } n   } n   cout << res <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   int q = 1; n   cin >> q; n   while (q--) query(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void add(long long &a, long long b) { n   a += b; n   if (a >= (233333333 * 4 + 23333333 * 2 + 2333333 * 8 + 233333 * 5 + n             23333 * 7 + 2333 + 233 * 4 + 23 * 3 + 2 + 2 + 2 + 3 + 3 + 3)) { n     a -= (233333333 * 4 + 23333333 * 2 + 2333333 * 8 + 233333 * 5 + 23333 * 7 + n           2333 + 233 * 4 + 23 * 3 + 2 + 2 + 2 + 3 + 3 + 3); n   } n } n struct BitchIsThere { n   int N; n   pair<long long, long long> dt[500005]; n   void upd(pair<long long, long long> &a, pair<long long, long long> b) { n     if (a.first > b.first) { n       a = b; n     } else if (a.first == b.first) { n       add(a.second, b.second); n     } n   } n   void init(int nn) { n     N = nn; n     for (int i = 1; i <= nn; i++) { n       dt[i] = make_pair( n           (233333333 * 4 + 23333333 * 2 + 2333333 * 8 + 233333 * 5 + 23333 * 7 + n            2333 + 233 * 4 + 23 * 3 + 2 + 2 + 2 + 3 + 3 + 3), n           0); n     } n   } n   void insert(int x, pair<long long, long long> num) { n     while (x <= N) { n       upd(dt[x], num); n       x += x & -x; n     } n   } n   pair<long long, long long> qsum(int x) { n     pair<long long, long long> ans = n         make_pair((233333333 * 4 + 23333333 * 2 + 2333333 * 8 + 233333 * 5 + n                    23333 * 7 + 2333 + 233 * 4 + 23 * 3 + 2 + 2 + 2 + 3 + 3 + 3), n                   0); n     while (x) { n       upd(ans, dt[x]); n       x -= x & -x; n     } n     return ans; n   } n } bit; n int n; n vector<pair<long long, long long> > pills; n int main() { n   scanf( %d , &n); n   vector<int> ds; n   ds.push_back(-1); n   ds.push_back(0); n   for (int i = 0; i < n; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     pills.push_back(make_pair(y, x)); n     ds.push_back(x); n     ds.push_back(y); n   } n   sort(ds.begin(), ds.end()); n   ds.resize(unique(ds.begin(), ds.end()) - ds.begin()); n   sort(pills.begin(), pills.end()); n   bit.init(ds.size()); n   bit.insert(1, make_pair(0, 1)); n   pair<long long, long long> ans = n       make_pair((233333333 * 4 + 23333333 * 2 + 2333333 * 8 + 233333 * 5 + n                  23333 * 7 + 2333 + 233 * 4 + 23 * 3 + 2 + 2 + 2 + 3 + 3 + 3), n                 0); n   for (pair<long long, long long> p : pills) { n     int x = lower_bound(ds.begin(), ds.end(), p.first) - ds.begin(); n     int y = lower_bound(ds.begin(), ds.end(), p.second) - ds.begin(); n     pair<long long, long long> res = bit.qsum(x); n     if (p.second > pills.back().first) { n       bit.upd(ans, make_pair(res.first + p.first, res.second)); n     } else { n       bit.insert(y, make_pair(p.first + res.first - p.second, res.second)); n     } n   } n   printf( %lld n , ans.second); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MAX = 101; n long long mat[MAX][MAX], ans[MAX][MAX], arr1[MAX], arr2[MAX]; n int main() { n   long long n, m, h; n   cin >> n >> m >> h; n   for (long long i = 0; i < m; i++) cin >> arr2[i]; n   for (long long i = 0; i < n; i++) cin >> arr1[i]; n   for (long long i = 0; i < n; i++) n     for (long long j = 0; j < m; j++) cin >> mat[i][j]; n   for (long long i = 0; i < m; i++) { n     for (long long j = 0; j < n; j++) n       if (arr2[i] <= arr1[j] && mat[j][i]) { n         ans[j][i] = arr2[i]; n         break; n       } n   } n   for (long long j = 0; j < n; j++) { n     for (long long i = 0; i < m; i++) n       if (arr1[j] <= arr2[i] && mat[j][i] && !ans[j][i]) { n         ans[j][i] = arr1[j]; n         break; n       } n   } n   for (long long i = 0; i < n; i++) { n     for (long long j = 0; j < m; j++) { n       if (mat[i][j] && !ans[i][j]) n         cout << 1 <<    ; n       else n         cout << ans[i][j] <<    ; n     } n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int s, v1, v2, t1, t2, q1, q2; n   cin >> s >> v1 >> v2 >> t1 >> t2; n   q1 = 2 * t1 + s * v1; n   q2 = 2 * t2 + s * v2; n   if (q1 < q2) { n     cout <<  First n ; n   } else if (q1 > q2) { n     cout <<  Second n ; n   } else { n     cout <<  Friendship n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int n, k, ans; n long long int check(long long int v) { n   long long int sum = 0; n   while (v > 0) { n     sum += v; n     v /= k; n   } n   return sum; n } n void BS(long long int l, long long int r) { n   while (l <= r) { n     long long int mid = (l + (r - l) / 2); n     long long int sum = check(mid); n     if (sum >= n) { n       r = mid - 1; n       ans = min(ans, mid); n     } else n       l = mid + 1; n   } n } n void solve() { n   cin >> n >> k; n   ans = n; n   long long int l, r; n   l = 1; n   r = n; n   BS(l, r); n   cout << ans <<   n ; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void qmax(long long &x, long long y) { n   if (x < y) x = y; n } n void qmin(long long &x, long long y) { n   if (x > y) x = y; n } n inline long long read() { n   char s; n   long long k = 0, base = 1; n   while ((s = getchar()) !=  -  && s != EOF && !(isdigit(s))) n     ; n   if (s == EOF) exit(0); n   if (s ==  - ) base = -1, s = getchar(); n   while (isdigit(s)) { n     k = k * 10 + (s ^  0 ); n     s = getchar(); n   } n   return k * base; n } n inline void write(long long x) { n   static char cnt, num[15]; n   cnt = 0; n   if (!x) { n     printf( 0 ); n     return; n   } n   for (; x; x /= 10) num[++cnt] = x % 10; n   for (; cnt; putchar(num[cnt--] + 48)) n     ; n } n long long n, l[70], x, f[70], ans[2001010]; n vector<long long> a[70]; n long long flag; n signed main() { n   n = read(); n   for (long long i = 1; i <= n; i++) { n     x = read(); n     for (long long i = 59; i >= 0; i--) { n       if (x & (1ll << i)) { n         a[i].push_back(x); n         l[i]++; n         break; n       } n     } n   } n   long long lst = 0; n   for (long long i = 0; i < 60; i++) n     if (l[i] != 0) sort(a[i].begin(), a[i].end()); n   for (long long j = 1; j <= n; j++) { n     flag = 0; n     for (long long i = 0; i < 60; i++) { n       if (f[i] == l[i]) continue; n       if (((lst >> (long long)i) & 1ll) != 0) continue; n       f[i]++; n       ans[j] = a[i][f[i] - 1]; n       flag = 1; n       lst ^= ans[j]; n       break; n     } n     if (!flag) break; n   } n   if (!flag) { n     printf( No ); n     return 0; n   } n   printf( Yes n ); n   for (long long i = 1; i <= n; i++) { n     printf( %lld  , ans[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct wut { n   int op; n   int val; n   int nval; n   wut() {} n   wut(int a, int b) : op(a), val(b) {} n }; n struct node { n   long long sum[5]; n   int ile; n   node() { n     for (int i = 0; i < (5); ++i) sum[i] = 0; n     ile = 0; n   } n }; n const int N = 100010; n wut tab[N]; n int n; n char tax[10]; n int c; n set<int> h; n map<int, int> et; n node tree[3 * N]; n int roz; n void insert(int value, int pos, bool add) { n   pos = roz + pos - 1; n   if (add) { n     tree[pos].sum[0] += value; n     tree[pos].ile++; n   } else { n     tree[pos].sum[0] -= value; n     tree[pos].ile--; n   } n   while (pos >> 1) { n     for (int i = 0; i < (5); ++i) n       tree[pos >> 1].sum[i] = tree[(pos >> 1) << 1].sum[i]; n     for (int i = 0; i < (5); ++i) { n       tree[pos >> 1].sum[i] += n           tree[((pos >> 1) << 1) + 1] n               .sum[(((i - tree[(pos >> 1) << 1].ile) % 5) + 5) % 5]; n     } n     tree[pos >> 1].ile = n         tree[(pos >> 1) << 1].ile + tree[((pos >> 1) << 1) + 1].ile; n     pos >>= 1; n   } n } n long long query() { return tree[1].sum[2]; } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < (n); ++i) { n     scanf( %s %d , tax, &c); n     if (tax[0] ==  a ) n       tab[i] = wut(0, c); n     else if (tax[0] ==  d ) n       tab[i] = wut(1, c); n     else n       tab[i] = wut(2, 0); n     if (tab[i].op <= 1) h.insert(tab[i].val); n   } n   int ind = 0; n   for (typeof((h).begin()) p = ((h).begin()); p != (h).end(); ++p) { n     et[*p] = ++ind; n   } n   for (int i = 0; i < (n); ++i) n     if (tab[i].op <= 1) { n       tab[i].nval = et[tab[i].val]; n     } n   roz = 1; n   while (roz < ind) roz <<= 1; n   for (int i = 0; i < (n); ++i) { n     if (tab[i].op == 2) { n       cout << query() << endl; n     } else { n       insert(tab[i].val, tab[i].nval, !tab[i].op); n     } n   } n } n


#include <bits/stdc++.h> n const int maxn = 1e5 + 10; n using namespace std; n int head[maxn], deep[maxn], k; n int f[maxn][23]; n int n, q, root; n struct edge { n   int v, next; n } e[maxn << 1]; n void adde(int u, int v) { n   e[k].v = v; n   e[k].next = head[u]; n   head[u] = k++; n } n void init() { n   k = 0; n   memset(head, -1, sizeof(head)); n   memset(f, 0, sizeof(f)); n   memset(deep, -1, sizeof(deep)); n } n void dfs(int u, int fa) { n   f[u][0] = fa; n   deep[u] = deep[fa] + 1; n   for (int j = 1; j <= 20; j++) f[u][j] = f[f[u][j - 1]][j - 1]; n   for (int i = head[u]; i != -1; i = e[i].next) { n     int v = e[i].v; n     if (v == fa) continue; n     dfs(v, u); n   } n } n int lca(int u, int v) { n   if (deep[u] > deep[v]) swap(u, v); n   for (int j = 20; j >= 0; j--) { n     if (deep[v] - (1 << j) >= deep[u]) v = f[v][j]; n   } n   if (u == v) return u; n   for (int j = 20; j >= 0; j--) { n     if (f[u][j] == f[v][j]) continue; n     u = f[u][j]; n     v = f[v][j]; n   } n   return f[u][0]; n } n int dis(int u, int v) { n   int ans = deep[u] + deep[v] - 2 * deep[lca(u, v)]; n   return ans; n } n int main() { n   init(); n   scanf( %d%d , &n, &q); n   int x; n   for (int i = 2; i <= n; i++) { n     scanf( %d , &x); n     adde(x, i); n     adde(i, x); n   } n   dfs(1, 0); n   int u1, u2, u3; n   while (q--) { n     scanf( %d%d%d , &u1, &u2, &u3); n     int lca1 = lca(u1, u2); n     int lca2 = lca(u1, u3); n     int lca3 = lca(u2, u3); n     if (deep[lca3] > deep[lca1]) lca1 = lca3; n     if (deep[lca2] > deep[lca1]) lca1 = lca2; n     int ans = max(dis(lca1, u3), max(dis(lca1, u2), dis(lca1, u1))) + 1; n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n using namespace std; n constexpr int LG = 60; n int N; n long long int ai, bi, bs; n long long int x[LG + 1]; n inline bool ins(long long int v) { n   for (int i = LG; i >= 0; --i) { n     if (!((v >> i) & 1)) continue; n     if (x[i]) { n       v ^= x[i]; n     } else { n       for (int j = i - 1; j >= 0; --j) n         if ((v >> j) & 1) v ^= x[j]; n       for (int j = i + 1; j <= LG; ++j) n         if ((x[j] >> i) & 1) x[j] ^= v; n       x[i] = v; n       return true; n     } n   } n   return false; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> N; n   int s = 0; n   for (int i = 0; i < N; ++i) { n     cin >> ai >> bi; n     bs ^= bi, ai ^= bi; n     if (ins(ai)) ++s; n   } n   for (int i = LG; i >= 0; --i) n     if ((bs >> i) & 1) bs ^= x[i]; n   if (bs) n     cout <<  1/1 n ; n   else n     cout << (1ll << s) - 1 <<  /  << (1ll << s) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n const int INF = 0x3f3f3f3f; n const int mod = 1e9 + 7; n const int maxn = 1e5 + 10; n using namespace std; n int a[105]; n int b[105]; n int main() { n   int n, m; n   scanf( %d %d , &n, &m); n   for (int i = 0; i < n; i++) { n     scanf( %d , &a[i]); n   } n   int x = 0; n   int y = 0; n   int cnt = 0; n   for (int i = 2; i != n; i += 2) { n     a[i - 2] % 2 == 1 ? x++ : y++; n     a[i - 1] % 2 == 1 ? x++ : y++; n     if (x == y) b[cnt++] = max(a[i], a[i - 1]) - min(a[i], a[i - 1]); n   } n   sort(b, b + cnt); n   int num = 0; n   for (int i = 0; i < cnt; i++) { n     if (m >= b[i]) { n       num++; n       m -= b[i]; n     } else n       break; n   } n   printf( %d n , num); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000007; n long long powmod(long long a, long long b) { n   long long res = 1; n   a %= mod; n   for (; b; b >>= 1) { n     if (b & 1) res = res * a % mod; n     a = a * a % mod; n   } n   return res; n } n const int N = 2010; n bitset<N> base[N], d, sel, Sel[N]; n bool bs[N]; n string p; n int n, a[N]; n void trans(string p) { n   d = 0; n   int l = ((int)(p).size()) / 8 + 1; n   reverse((p).begin(), (p).end()); n   while (((int)(p).size()) <= 8 * l) p.push_back( 0 ); n   for (int i = 0; i < l; i++) { n     a[i] = 0; n     for (int j = 8 - 1; j >= 0; j--) a[i] = a[i] * 10 + p[8 * i + j] -  0 ; n   } n   long long s = 0; n   int tot = 0; n   while (l) { n     s = 0; n     for (int i = l - 1; i >= 0; i--) { n       s = s * 100000000 + a[i]; n       a[i] = s >> 32; n       s = s % (1ll << 32); n     } n     for (int i = 0; i < 32; i++) d[tot++] = (s >> i) & 1; n     while (l && a[l - 1] == 0) --l; n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     cin >> p; n     trans(p); n     sel = 0; n     bool sol = 0; n     for (int k = 2000; k >= 0; k--) n       if (d[k]) { n         if (bs[k]) { n           d = d ^ base[k]; n           sel ^= Sel[k]; n         } else { n           sol = 1; n           sel[i].flip(); n           bs[k] = 1; n           base[k] = d; n           Sel[k] = sel; n           break; n         } n       } n     if (sol) n       puts( 0 ); n     else { n       int cnt = 0; n       for (int j = 0; j < i; j++) n         if (sel[j]) cnt++; n       printf( %d , cnt); n       for (int j = 0; j < i; j++) n         if (sel[j]) printf(  %d , j); n       puts(  ); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long f[200]; n int main() { n   int n; n   cin >> n; n   f[1] = 0; n   for (int i = 0; i < 101; i++) { n     f[i] = f[i - 1] + (i / 2); n   } n   cout << f[n] << endl; n   for (int i = 1; i <= n; i++) { n     int x = i - 1; n     while (x > 0) { n       cout << i <<     << x; n       x -= 2; n       cout << endl; n     } 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 n, k = 0; n   cin >> n; n   int arr[n], brr[n], crr[n]; n   for (int i = 0; i < n; i++) { n     cin >> arr[i] >> brr[i]; n     crr[i] = arr[i]; n   } n   sort(crr, crr + n); n   for (int i = 0; i < n; i++) { n     int upper = upper_bound(crr, crr + n, brr[i]) - crr; n     int lower = lower_bound(crr, crr + n, brr[i]) - crr; n     cout << n - 1 + upper - lower <<     << n - 1 + lower - upper << endl; n   } n } n


#include <bits/stdc++.h> n inline char gc() { n   static char buf[10000], *p1 = buf, *p2 = buf; n   return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 10000, stdin), p1 == p2) n              ? EOF n              : *p1++; n } n inline long long read() { n   long long x = 0; n   int f = 1; n   char c = gc(); n   for (; !isdigit(c); c = gc()) n     if (c ==  - ) f = -1; n   for (; isdigit(c); c = gc()) x = (x << 1) + (x << 3) + (c ^ 48); n   return x * f; n } n inline void write(long long x) { n   if (x < 0) putchar( - ), x = -x; n   if (x > 9) write(x / 10); n   putchar(x % 10 | 48); n } n const int N = 1e3 + 10; n long long V, ans, A, B, C; n int n; n struct node { n   long long s; n   int num; n   inline bool operator<(const node& res) const { return s < res.s; } n } s[N]; n inline long long pow(long long x, int y) { n   if (y == 1) return x; n   long long k = 1; n   for (; y; y >>= 1, x = x * x) n     if (y & 1) k = k * x; n   return k; n } n inline void dfs1(long long a, int x, long long b) { n   if (!x) { n     if (b < a) return; n     long long c = V / b / a; n     if (a * b + a * c + b * c < ans) { n       ans = a * b + a * c + b * c; n       A = a, B = b, C = c; n     } n     return; n   } n   for (int i = 0; i <= s[x].num; ++i) { n     if (a * b * 2 + b * b >= ans || a * b * b > V) return; n     dfs1(a, x - 1, b); n     if (i == s[x].num) break; n     b *= s[x].s; n   } n   return; n } n inline void dfs(int x, long long a) { n   if (x > n) { n     double tem = sqrt(V / a); n     if (tem * tem + a * tem * 2 >= ans) return; n     dfs1(a, n, 1); n     return; n   } n   for (register int i = 0; i <= s[x].num; ++i) { n     if (a * a * 3 >= ans || a * a * a > V) return; n     s[x].num -= i; n     dfs(x + 1, a); n     s[x].num += i; n     if (i == s[x].num) break; n     a *= s[x].s; n   } n   return; n } n int main() { n   for (register int T = read(); T; --T) { n     n = read(); n     V = 1; n     for (register int i = 1; i <= n; ++i) n       s[i].s = read(), s[i].num = read(), V *= pow(s[i].s, s[i].num); n     std::sort(s + 1, s + 1 + n); n     ans = V << 1 | 1; n     A = B = 1, C = V; n     dfs(1, 1); n     write(ans << 1), putchar(   ); n     write(A), putchar(   ); n     write(B), putchar(   ); n     write(C), puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, k, p, q, x = 0, y = 0, ans; n   cin >> n >> k; n   p = q = n; n   while (p % 2 == 0) { n     p /= 2; n     x++; n   } n   while (q % 5 == 0) { n     q /= 5; n     y++; n   } n   p = k - x; n   q = k - y; n   if (p < 0) p = 0; n   if (q < 0) q = 0; n   ans = n * pow(2, p) * pow(5, q); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 2000000000; n const int N = 1e5 + 5; n bool ok = false; n int mid, lx, ly, first, second, n, down, up, level[N]; n vector<pair<int, int>> g[N]; n int dfs(int v, int size, int &center, int pr = 0) { n   int sum = 1; n   for (int i = 0; i < g[v].size(); i++) { n     int to = g[v][i].first; n     if (to != pr && level[to] == -1) { n       sum += dfs(to, size, center, v); n     } n   } n   if (center == -1 && sum >= size / 2) { n     center = v; n   } n   return sum; n } n void dfscalc(int v, int sum, int len, vector<pair<int, int>> &cur, n              int pr = -1) { n   if (len > cur.size()) { n     cur.push_back(make_pair(sum, v)); n   } else { n     if (sum > cur[len - 1].first) { n       cur[len - 1] = make_pair(sum, v); n     } n   } n   if (len < up) { n     for (int i = 0; i < g[v].size(); i++) { n       int to = g[v][i].first; n       int val = (g[v][i].second >= mid ? 1 : -1); n       if (to != pr && level[to] == -1) { n         dfscalc(to, sum + val, len + 1, cur, v); n       } n     } n   } n } n bool cmp(vector<pair<int, int>> &a, vector<pair<int, int>> &b) { n   return (a.size() < b.size()); n } n void upd(pair<int, int> tree[], int first, pair<int, int> second) { n   if (second.first > tree[first].first) { n     tree[first] = second; n   } n   while (first > 1) { n     first >>= 1; n     if (tree[first + first].first > tree[first + first + 1].first) { n       tree[first] = tree[first + first]; n     } else { n       tree[first] = tree[first + first + 1]; n     } n   } n } n pair<int, int> getmax(pair<int, int> tree[], int v, int lv, int rv, int l, n                       int r) { n   if (lv > r || rv < l) { n     return {-inf, -1}; n   } n   if (lv >= l && rv <= r) { n     return tree[v]; n   } n   int mid = (lv + rv) / 2; n   pair<int, int> q = getmax(tree, v + v, lv, mid, l, r); n   pair<int, int> w = getmax(tree, v + v + 1, mid + 1, rv, l, r); n   if (q.first > w.first) { n     return q; n   } else { n     return w; n   } n } n void calc(int v, int size) { n   vector<vector<pair<int, int>>> a; n   for (int i = 0; i < g[v].size(); i++) { n     int to = g[v][i].first; n     int val = g[v][i].second >= mid ? 1 : -1; n     if (level[to] == -1) { n       vector<pair<int, int>> cur; n       dfscalc(to, val, 1, cur, v); n       a.push_back(cur); n     } n   } n   if (a.size() == 0) { n     return; n   } n   sort((a).begin(), (a).end(), cmp); n   int treesize = size; n   int sz = 1; n   while (sz < treesize) { n     sz <<= 1; n   } n   pair<int, int> tree[2 * sz]; n   for (int i = 1; i < 2 * sz; i++) { n     tree[i] = {-inf, -1}; n   } n   for (int i = 0; i < a.size(); i++) { n     for (int j = 0; j < a[i].size(); j++) { n       int len = j + 1; n       if (a[i][j].first >= 0) { n         if (len >= down && len <= up) { n           ok = true; n           first = v; n           second = a[i][j].second; n           return; n         } n       } n       if (len < up && a.size() > 1) { n         pair<int, int> tmp = n             getmax(tree, 1, 1, sz, max(1, down - len), up - len); n         if (a[i][j].first + tmp.first >= 0) { n           ok = true; n           first = tmp.second; n           second = a[i][j].second; n           return; n         } n       } n     } n     if (a.size() > 1) { n       for (int j = 0; j < a[i].size(); j++) { n         int len = j + 1; n         upd(tree, len + sz - 1, {a[i][j].first, a[i][j].second}); n       } n     } n   } n } n void build(int v, int depth = 1) { n   int tmp = 0, center = -1; n   int size = dfs(v, 0, tmp); n   dfs(v, size, center); n   if (size < down) { n     return; n   } n   level[center] = depth; n   calc(center, size / 2 + 1); n   for (int i = 0; i < g[center].size() && !ok; i++) { n     int to = g[center][i].first; n     if (level[to] == -1) { n       build(to, depth + 1); n     } n   } n } n int main() { n   cin.tie(0); n   ios_base::sync_with_stdio(0); n   cin >> n >> down >> up; n   int mx = 0; n   for (int i = 1; i < n; i++) { n     int q, w, c; n     cin >> q >> w >> c; n     g[q].push_back(make_pair(w, c)); n     g[w].push_back(make_pair(q, c)); n     mx = max(mx, c); n   } n   int l = 0; n   int r = mx; n   int lx, ly; n   while (l <= r) { n     memset(level, -1, sizeof(level)); n     mid = (l + r) / 2; n     ok = false; n     build(1); n     if (ok) { n       l = mid + 1; n       lx = first; n       ly = second; n     } else { n       r = mid - 1; n     } n   } n   cout << lx <<     << ly <<   n ; 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     long long w; n     cin >> n >> w; n     pair<long long, int> wt[n]; n     for (int i = 0; i < n; i++) { n       cin >> wt[i].first; n       wt[i].second = i + 1; n     } n     vector<int> taken; n     sort(wt, wt + n); n     long long current = 0; n     for (int i = n - 1; i >= 0; i--) { n       if (current + wt[i].first <= w) { n         taken.push_back(wt[i].second); n         current += wt[i].first; n       } n       assert(current <= w); n     } n     if (current * 2 >= w) { n       cout << taken.size() <<   n ; n       for (int i : taken) cout << i <<    ; n       cout <<   n ; n     } else { n       cout <<  -1 n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Maxn = 1e4 + 100; n struct Date { n   int x, y; n   bool z; n }; n stack<Date> S; n int n, m, x, y, flag; n struct dsu { n   int f[Maxn]; n   bool odd[Maxn]; n   void clear() { n     for (int i = 1; i <= n; i++) f[i] = i, odd[i] = false; n   } n   int find(int x, int w) { n     if (x == f[x]) return x; n     if (w) S.push((Date){x, f[x], odd[x]}); n     int ans = find(f[x], w); n     odd[x] ^= odd[f[x]]; n     return f[x] = ans; n   } n   bool merge(int x, int y, int w) { n     int u = find(x, w), v = find(y, w); n     if (u == v) return odd[x] == odd[y]; n     if (w) S.push((Date){u, f[u], odd[u]}); n     f[u] = y; n     odd[u] = odd[x] ^ 1; n     return false; n   } n   void restore() { n     while (!S.empty()) { n       Date i = S.top(); n       f[i.x] = i.y; n       odd[i.x] = i.z; n       S.pop(); n     } n   } n } Dsu; n struct Edge { n   int from, to; n }; n vector<Edge> edges; n vector<int> ans; n int main() { n   scanf( %d %d , &n, &m); n   if (m == 0) { n     printf( 0 ); n     return 0; n   } n   for (int i = 1; i <= m; i++) { n     scanf( %d %d , &x, &y); n     edges.push_back((Edge){x, y}); n   } n   int Len = sqrt(m + 0.5), N = (m - 1) / Len + 1; n   for (int i = 1; i <= N; i++) { n     Dsu.clear(); n     flag = false; n     for (int j = 0; j < m; j++) { n       if (j < i * Len && j >= (i - 1) * Len) continue; n       int from = edges[j].from, to = edges[j].to; n       if (Dsu.merge(from, to, 0)) { n         flag = true; n         break; n       } n     } n     if (flag) continue; n     for (int j = (i - 1) * Len; j < min(i * Len, m); j++) { n       for (int k = (i - 1) * Len; k < min(i * Len, m); k++) { n         if (j == k) continue; n         if (Dsu.merge(edges[k].from, edges[k].to, 1)) { n           flag = true; n           break; n         } n       } n       if (!flag) ans.push_back(j + 1); n       Dsu.restore(); n       flag = false; n     } n   } n   printf( %d n , ans.size()); n   for (int i = 0; i < ans.size(); i++) printf( %d  , ans[i]); n } n


#include <bits/stdc++.h> n const int N = 110000; n using namespace std; n set<pair<int, int> > elements; n set<pair<int, int> >::iterator it; n int n; n int ar[N]; n int next_lucky[N], prev_lucky[N]; n bool is_lucky[N]; n vector<int> luck; n long long ans; n long long BIN[N]; n set<int> banned_set; n set<pair<int, int> > seg; n int banned[N]; n long long cur_cnt; n long long bin_sum[N]; n bool lucky(int x) { n   if (x == 0) return 1; n   if (x % 10 != 4 && x % 10 != 7) return 0; n   return lucky(x / 10); n } n void add_segment(int l, int r) { n   cur_cnt += BIN[r - l + 1]; n   cur_cnt %= 5000000000000000000ll; n } n void remove_segment(int l, int r) { n   cur_cnt -= BIN[r - l + 1]; n   if (cur_cnt < 0) cur_cnt += 5000000000000000000ll; n } n void ban_position(int ps) { n   set<int>::iterator it; n   it = banned_set.lower_bound(ps); n   int r = (*it); n   --it; n   int l = (*it); n   remove_segment(l + 1, r - 1); n   if (l < ps - 1) add_segment(l + 1, ps - 1); n   if (r > ps + 1) add_segment(ps + 1, r - 1); n   banned_set.insert(ps); n } n void show_intervals() { n   set<pair<int, int> >::iterator it; n   for (it = seg.begin(); it != seg.end(); ++it) { n     pair<int, int> P = (*it); n     cout << P.first <<     << P.second << endl; n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   for (int i = 1; i <= 100000; i++) n     BIN[i] = (i + 1) * 1ll * i / 2, BIN[i] %= 5000000000000000000ll; n   for (int i = 1; i <= 100000; i++) { n     bin_sum[i] = bin_sum[i - 1] + BIN[i]; n     bin_sum[i] %= 5000000000000000000ll; n   } n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> ar[i]; n     elements.insert(make_pair(ar[i], i)); n     if (lucky(ar[i])) { n       is_lucky[i] = 1; n       luck.push_back(i); n     } n   } n   next_lucky[n + 1] = n + 1; n   for (int i = n; i; --i) { n     if (is_lucky[i]) n       next_lucky[i] = i; n     else n       next_lucky[i] = next_lucky[i + 1]; n   } n   for (int i = 1; i <= n; i++) { n     int cnt1 = next_lucky[i] - i; n     ans += cnt1 * BIN[i - 1] % 5000000000000000000ll, n         ans %= 5000000000000000000ll; n   } n   for (int i = 1; i <= n; i++) { n     if (is_lucky[i]) n       prev_lucky[i] = i; n     else n       prev_lucky[i] = prev_lucky[i - 1]; n   } n   for (int i = 0; i < luck.size(); i++) { n     int id = luck[i]; n     seg.clear(); n     cur_cnt = 0; n     add_segment(1, id - 1); n     for (int j = 0; j < luck.size(); j++) banned[j] = 0; n     banned_set.clear(); n     banned_set.insert(id); n     banned_set.insert(0); n     for (int j = i; j < luck.size(); j++) { n       int id = luck[j]; n       int val = ar[id]; n       int pp = -1; n       while (true) { n         pair<int, int> P = make_pair(val, pp + 1); n         it = elements.lower_bound(P); n         if (it == elements.end()) break; n         pair<int, int> P2 = (*it); n         if (P2.first != P.first) break; n         if (P2.second >= luck[i]) break; n         if (banned_set.find(P2.second) != banned_set.end()) break; n         ban_position(P2.second); n         pp = P2.second; n       } n       int bound = n + 1; n       if (j + 1 < luck.size()) bound = luck[j + 1]; n       long long ways = cur_cnt; n       int prev = 0; n       if (i) prev = luck[i - 1]; n       ways = ways * (luck[i] - prev) % 5000000000000000000ll; n       set<int>::iterator it; n       it = banned_set.lower_bound(luck[i]); n       int Q1, Q2; n       if (it == banned_set.begin()) n         Q1 = 0; n       else { n         --it; n         Q1 = (*it); n       } n       Q2 = luck[i]; n       ways -= (BIN[Q2 - Q1 - 1]) * (luck[i] - prev) % 5000000000000000000ll; n       if (ways < 0) ways += 5000000000000000000ll; n       int cut; n       if (i == 0) n         cut = 0; n       else n         cut = luck[i - 1]; n       ways += bin_sum[Q2 - Q1 - 1] - bin_sum[cut - Q1 - 1]; n       ways %= 5000000000000000000ll; n       if (ways < 0) ways += 5000000000000000000ll; n       ways = ways * (bound - luck[j]) % 5000000000000000000ll; n       ; n       ans += ways; n       ans %= 5000000000000000000ll; n     } n   } n   cout << ans << endl; n   cin.get(); n   cin.get(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, xa, ya; n int i, j, viz[501][501], light[501][501], d[501][501]; n string ans; n void paint(int x, int y) { n   viz[x][y] = 1; n   if (light[x][y] == 0) light[x][y] = 1, ans +=  1 ; n   int up, down, right, left; n   up = down = right = left = 0; n   for (j = 1; j < y; j++) n     if (light[x][j]) left = 1; n   for (j = y + 1; j <= n; j++) n     if (light[x][j]) right = 1; n   for (i = 1; i < x; i++) n     if (light[i][y]) up = 1; n   for (i = x + 1; i <= n; i++) n     if (light[i][y]) down = 1; n   if (up && !viz[x - 1][y]) ans +=  U , paint(x - 1, y), ans +=  D ; n   if (down && !viz[x + 1][y]) ans +=  D , paint(x + 1, y), ans +=  U ; n   if (left && !viz[x][y - 1]) ans +=  L , paint(x, y - 1), ans +=  R ; n   if (right && !viz[x][y + 1]) ans +=  R , paint(x, y + 1), ans +=  L ; n }; n void bfs() { n   queue<pair<int, int> > q; n   q.push(make_pair(xa, ya)); n   for (i = 1; i <= n; i++) n     for (j = 1; j <= n; j++) viz[i][j] = 0, d[i][j] = -1; n   d[xa][ya] = 0; n   while (!q.empty()) { n     pair<int, int> k = q.front(); n     q.pop(); n     if (light[k.first + 1][k.second] && d[k.first + 1][k.second] == -1) { n       d[k.first + 1][k.second] = d[k.first][k.second] + 1; n       q.push(make_pair(k.first + 1, k.second)); n     }; n     if (light[k.first - 1][k.second] && d[k.first - 1][k.second] == -1) { n       d[k.first - 1][k.second] = d[k.first][k.second] + 1; n       q.push(make_pair(k.first - 1, k.second)); n     }; n     if (light[k.first][k.second + 1] && d[k.first][k.second + 1] == -1) { n       d[k.first][k.second + 1] = d[k.first][k.second] + 1; n       q.push(make_pair(k.first, k.second + 1)); n     }; n     if (light[k.first][k.second - 1] && d[k.first][k.second - 1] == -1) { n       d[k.first][k.second - 1] = d[k.first][k.second] + 1; n       q.push(make_pair(k.first, k.second - 1)); n     }; n   }; n }; n void repaint(int x, int y) { n   viz[x][y] = 1; n   if (light[x - 1][y] && !viz[x - 1][y] && d[x - 1][y] > d[x][y]) n     ans +=  U , repaint(x - 1, y), ans +=  D ; n   if (light[x + 1][y] && !viz[x + 1][y] && d[x + 1][y] > d[x][y]) n     ans +=  D , repaint(x + 1, y), ans +=  U ; n   if (light[x][y - 1] && !viz[x][y - 1] && d[x][y - 1] > d[x][y]) n     ans +=  L , repaint(x, y - 1), ans +=  R ; n   if (light[x][y + 1] && !viz[x][y + 1] && d[x][y + 1] > d[x][y]) n     ans +=  R , repaint(x, y + 1), ans +=  L ; n   if (light[x][y] == 1) light[x][y] = 0, ans +=  2 ; n }; n int main() { n   cin >> n >> xa >> ya; n   for (i = 1; i <= n; i++) n     for (j = 1; j <= n; j++) cin >> light[i][j]; n   paint(xa, ya); n   for (i = 1; i <= n; i++) n     for (j = 1; j <= n; j++) n       if (viz[i][j] == 0 && light[i][j] == 1) { n         cout <<  NO ; n         return 0; n       }; n   cout <<  YES  << endl; n   bfs(); n   repaint(xa, ya); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000010; n const int mod = 998244353; n const int inv = 332748118; n int n; n long long ans, js[N], ni[N]; n inline long long C(int x, int y) { n   if (x < 0 || y < 0 || x < y) return 0; n   return js[x] * ni[y] % mod * ni[x - y] % mod; n } n inline long long qpow(long long x, long long y, long long ans = 1) { n   for (; y; y >>= 1, x = x * x % mod) n     if (y & 1) ans = ans * x % mod; n   return ans; n } n int main() { n   cin >> n; n   for (int i = js[0] = 1; i <= n; ++i) js[i] = js[i - 1] * i % mod; n   for (int i = (ni[n] = qpow(js[n], mod - 2), n); i; --i) n     ni[i - 1] = ni[i] * i % mod; n   for (int i = 0; i <= n; ++i) { n     long long f; n     if (!i) n       f = qpow(qpow(3, n) - 3, n); n     else n       f = ((i & 1) ? -1 : 1) * C(n, i) * n           (3 * qpow(qpow(3, n - i) - 1, n) % mod + n            (qpow(3, i) - 3) * qpow(3, 1ll * (n - i) * n) % mod) % n           mod; n     (ans += f) %= mod; n   } n   cout << ((qpow(3, 1ll * n * n % (mod - 1)) - ans) % mod + mod) % mod << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool f[501][501]; n long long P[501]; n int main(int argc, char *argv[]) { n   int N, M, i, u, v, k, l; n   cin >> N >> M; n   for (i = 1; i <= N; i++) cin >> P[i]; n   for (i = 1; i <= N; i++) n     for (k = 1; k <= N; k++) f[i][k] = false; n   for (i = 1; i <= M; i++) { n     cin >> u >> v; n     f[u][v] = true; n     f[v][u] = true; n   } n   long long mini = (1 << 29); n   long long sum; n   for (int i = 1; i <= N - 2; i++) { n     for (int j = i + 1; j <= N - 1; j++) { n       for (int k = j + 1; k <= N; k++) { n         if (f[i][j] && f[j][k] && f[k][i]) { n           sum = P[i] + P[j] + P[k]; n           if (sum < mini) mini = sum; n         } n       } n     } n   } n   if (mini != (1 << 29)) n     cout << mini << endl; n   else n     cout << -1 << endl; 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 > 0) { n     int n; n     cin >> n; n     while (n > 0) { n       cout << 1 <<    ; n       n--; n     } n     cout <<   n ; n     t--; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n class ST { n   long long int tree[10000000]; n   long long int lazy[10000000]; n  n  public: n   void build(int a, int b, int node) { n     tree[node] = 0; n     lazy[node] = 0; n     if (a == b) return; n     int mid = (a + b + 4000000) / 2 - 2000000; n     build(a, mid, 2 * node); n     build(mid + 1, b, 2 * node + 1); n   } n   void propagate(int a, int b, int node) { n     tree[node] += lazy[node]; n     if (a != b) { n       lazy[2 * node] += lazy[node]; n       lazy[2 * node + 1] += lazy[node]; n     } n     lazy[node] = 0; n   } n   void update(int a, int b, int node, int x, int y, long long int val) { n     propagate(a, b, node); n     if (b < x || y < a) return; n     if (x <= a && b <= y) { n       lazy[node] += val; n       propagate(a, b, node); n       return; n     } n     int mid = (a + b + 4000000) / 2 - 2000000; n     update(a, mid, 2 * node, x, y, val); n     update(mid + 1, b, 2 * node + 1, x, y, val); n     tree[node] = max(tree[2 * node], tree[2 * node + 1]); n   } n   long long int query() { return tree[1]; } n }; n int main() { n   int n; n   long long int r; n   scanf( %d %lld , &n, &r); n   vector<pair<long long int, long long int> > V; n   for (int i = 0; i < n; i++) { n     long long int a, b; n     scanf( %lld %lld , &a, &b); n     V.push_back(pair<long long int, long long int>(a + b, a - b)); n   } n   ST *S = new ST(); n   S->build(-2000000, 2000000, 1); n   sort(V.begin(), V.end()); n   int ptr = 0; n   long long int ans = 0; n   for (int i = 0; i < n; i++) { n     while (V[ptr].first < V[i].first - 2 * r) { n       S->update(-2000000, 2000000, 1, V[ptr].second - 2 * r, V[ptr].second, -1); n       ptr++; n     } n     S->update(-2000000, 2000000, 1, V[i].second - 2 * r, V[i].second, 1); n     ans = max(ans, S->query()); n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n, amount(0); n   scanf( %d , &n); n   bool b[3001] = {false}; n   int pfactors[3001] = {0}; n   for (int i = 2; i <= n; ++i) { n     if (!b[i]) { n       for (int j = i + i; j <= n; j += i) { n         b[j] = true; n         pfactors[j] += 1; n       } n     } n     if (pfactors[i] == 2) { n       amount += 1; n     } n   } n   printf( %d n , amount); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 105; n const int mod = 1e9 + 7; n inline void MOD(int &x) { n   if (x >= mod) x -= mod; n } n int n, m, k; n int dp[2][N][N][N], C[N][N], r; n int ans; n int main() { n   for (int i = 0; i <= 100; i++) { n     C[i][0] = 1; n     for (int j = 1; j <= i; j++) n       C[i][j] = min(C[i - 1][j - 1] + C[i - 1][j], 101); n   } n   scanf( %d%d%d , &n, &m, &k); n   r = 0; n   for (int j = 2; j <= n + 1; j++) dp[r][j][j - 1][1] = 1; n   for (int i = 1; i <= m; i++) { n     for (int j = 1 + i; j <= n + 1; j++) n       for (int q = 1; q <= k; q++) n         ans = ((long long)dp[r][j][0][q] * (m - i + 1) + ans) % (long long)mod, n         dp[r][j][0][q] = 0; n     if (i == m) break; n     for (int j = 1 + i; j <= n + 1; j++) n       for (int p = 1; p < j; p++) n         for (int q = 1; q <= k; q++) n           if (dp[r][j][p][q]) { n             for (int t = p; j + t <= n + 1 && q * C[t - 1][p - 1] <= k; t++) n               MOD(dp[r ^ 1][j + t][t - p][q * C[t - 1][p - 1]] += n                   dp[r][j][p][q]); n             dp[r][j][p][q] = 0; n           } n     r ^= 1; n   } n   printf( %d n , ans); n   return 0; n } n


#include<bits/stdc++.h> n  n using namespace std; n  n /* User Template n @Shaurya Gautam n CP.cpp */ n  n /*****************************************/ n  n #define ll long long n #define vb vector<bool> n #define vi vector<int> n #define vl vector<long> n #define vll vector<long long> n #define vpii vector<pair<int, int>> n #define vc vector<char> n #define pii pair<int, int> n #define mp make_pair n #define F(i,L,R) for (int i = L; i < R; i++) n #define FE(i,L,R) for (int i = L; i <= R; i++) n #define FR(i,L,R) for (int i = L; i > R; i--) n #define FRE(i,L,R) for (int i = L; i >= R; i--) n #define pb push_back n #define ppb pop_back n #define endl   n  n  n class my_compare{ n     public: n         bool operator()(pii &p1, pii &p2){ n             return p1.second < p2.second; n         } n }; n  n  n /*************************************************/ n void solve(){ n     int n; n     cin >> n; n  n     vi arr(n + 1); n     FE(i, 1, n){ n         cin >> arr[i]; n     } n  n     int ans{0}; n  n     FE(i, 1, n){ n         if(arr[i] > 2*i - 1) continue; n          n         int j = (arr[i] > i) ? 1 : (i/arr[i]) + 1; n         while(j*arr[i] <= 2*i - 1){ n             if(arr[j*arr[i] - i] == j) ans++; n             j++; n         } n     } n  n     cout << ans << endl; n } n  n int main(){ n     ios_base::sync_with_stdio(false); n     cin.tie(NULL); n     cout.tie(NULL); n  n  n     bool test_case{1}, fileop{0}; n  n     if(fileop){ n         freopen( input.txt , r ,stdin); n         freopen( output.txt , w ,stdout); n     } n  n     int t; n     if(test_case){ n         cin >> t; n         F(i, 0, t){ n             solve(); n         } n     } n     else solve(); n } n  n /*  n Some Learnings- n      -Don t focus on ratings or standings n      -See problem from new perspective and forget current thoughts  n      -Accuracy is important, be sure that you think right n      -Speed builds up once accuracy does... solve as many questions as possible  n      -Editorial is last resort... don t jump to it n      -BE CONSISTENT n      -My two brothers - Hard Work and Perseverance n      -Never leave a contest, no matter how tough it might be n */


#include <bits/stdc++.h> n using namespace std; n int n; n vector<int> g[100005]; n int c[100005]; n double r[100005]; n void dfs(int x) { n   c[x] = 1; n   for (int i = 0; i < g[x].size(); ++i) { n     int y = g[x][i]; n     dfs(y); n     c[x] += c[y]; n   } n } n void calc(int x) { n   for (int i = 0; i < g[x].size(); ++i) { n     int y = g[x][i]; n     r[y] = r[x] + 1 + (c[x] - c[y] - 1) / 2.; n     calc(y); n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i < n; ++i) { n     int p; n     scanf( %d , &p); n     p--; n     g[p].push_back(i); n   } n   dfs(0); n   r[0] = 1; n   calc(0); n   printf( 1.0 ); n   for (int i = 1; i < n; ++i) printf(  %.1lf , r[i]); n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N; n int V[100]; n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int T; n   cin >> T; n   while (T--) { n     int K; n     cin >> K; n     const int R = 2 * K + 1; n     N = 2 * R; n     int p0 = -1; n     for (int i = 0; i < N; i++) { n       string s; n       cin >> s; n       if (s ==  E ) { n         V[i] = 0; n         p0 = i; n       } else { n         V[i] = stoi(s); n       } n     } n     assert(p0 != -1); n     string ans; n     while (p0 % R) { n       ans +=  l ; n       swap(V[p0], V[p0 - 1]); n       p0--; n     } n     if (p0 == R) { n       ans +=  u ; n       swap(V[p0], V[p0 - R]); n       p0 -= R; n     } n     assert(p0 == 0); n     bool parity = 0; n     for (int i = 0; i < N; i++) { n       for (int j = i + 1; j < N; j++) { n         if (V[j] < V[i]) parity ^= 1; n       } n     } n     if (parity) { n       cout <<  SURGERY FAILED  <<   n ; n       continue; n     } n     for (int i = 0; i < K; i++) { n       ans +=  r ; n       swap(V[p0], V[p0 + 1]); n       p0++; n     } n     assert(p0 == K); n     ans +=  d ; n     swap(V[p0], V[p0 + R]); n     p0 += R; n     auto VtoF = [&](int v) { n       assert(v != 0); n       if (v <= K) { n         return v + K; n       } else if (v == K + 1) { n         return R; n       } else if (v <= R) { n         return 2 * R - (v - (K + 1)); n       } else if (v <= R + K) { n         return (K + 1) - (v - R); n       } else { n         return R + (v - R - K); n       } n     }; n     deque<int> lhs; n     deque<int> rhs; n     for (int i = K - 1; i >= 0; i--) { n       lhs.push_front(VtoF(V[i])); n     } n     for (int i = 0; i < K; i++) { n       lhs.push_front(VtoF(V[R + i])); n     } n     for (int i = K + 1; i < R; i++) { n       rhs.push_front(VtoF(V[i])); n     } n     for (int i = R - 1; i > K; i--) { n       rhs.push_front(VtoF(V[R + i])); n     } n     int mid = VtoF(V[K]); n     assert(int(lhs.size()) == 2 * K); n     assert(int(rhs.size()) == 2 * K); n     auto rotL = [&]() { n       ans +=  L ; n       lhs.push_back(mid); n       mid = lhs.front(); n       lhs.pop_front(); n     }; n     auto unrotL = [&]() { n       ans +=  X ; n       lhs.push_front(mid); n       mid = lhs.back(); n       lhs.pop_back(); n     }; n     auto rotR = [&]() { n       ans +=  R ; n       rhs.push_back(mid); n       mid = rhs.front(); n       rhs.pop_front(); n     }; n     auto unrotR = [&]() { n       ans +=  Y ; n       rhs.push_front(mid); n       mid = rhs.back(); n       rhs.pop_back(); n     }; n     for (int z = 0; z < 2 * N; z++) { n       if (mid <= R) { n         rotL(); n       } else { n         rotR(); n       } n     } n     for (int z = 0; z < R; z++) { n       rotL(); n       for (int i = 0; i < R - 1; i++) { n         if (lhs.front() < mid) { n           rotR(); n           rotL(); n           unrotR(); n           unrotL(); n           rotR(); n         } n         rotL(); n       } n     } n     assert(mid == R); n     mid = 2 * R; n     bool f = false; n     for (int z = 0; z < R; z++) { n       rotR(); n       for (int i = 0; i < R - 1; i++) { n         if (rhs.front() < mid) { n           f ? unrotL() : rotL(); n           rotR(); n           f ? rotL() : unrotL(); n           unrotR(); n           f ? unrotL() : rotL(); n           f = !f; n         } n         rotR(); n       } n     } n     assert(f == false); n     for (int i = 0; i < K; i++) { n       ans +=  r ; n     } n     cout <<  SURGERY COMPLETE  <<   n ; n     cout << ans <<   n ; n     cout <<  L  <<     << string(K,  l ) +  u  + string(K,  r ) +  d  <<   n ; n     cout <<  X  <<     <<  u  + string(K,  l ) +  d  + string(K,  r ) <<   n ; n     cout <<  R  <<     << string(K,  r ) +  u  + string(K,  l ) +  d  <<   n ; n     cout <<  Y  <<     <<  u  + string(K,  r ) +  d  + string(K,  l ) <<   n ; n     cout <<  DONE  <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, a, b, c, d, x, y, v, ans = 0; n   cin >> n >> a >> b >> c >> d; n   for (long long i = 1; i <= n; i++) { n     x = a - d + i; n     v = b - c + i; n     y = a + b - c - d + i; n     if ((x > 0 && x <= n) && (y > 0 && y <= n) && (v > 0 && v <= n)) ans += n; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f, mod = 1000000007; n const long long INF = 0x3f3f3f3f3f3f3f3fLL; n const double pi = acosl(-1.), eps = 1e-9; n inline int power(int a, int b, int m = mod, int ans = 1) { n   for (; b; b >>= 1, a = 1LL * a * a % m) n     if (b & 1) ans = 1LL * ans * a % m; n   return ans; n } n int que(vector<int> a, vector<int> b) { n   printf( %d %d  , a.size(), b.size()); n   for (auto r : a) cout << r <<    ; n   for (auto r : b) cout << r <<    ; n   fflush(stdout); n   int ans; n   cin >> ans; n   return ans; n } n int main() { n   int T; n   cin >> T; n   while (T--) { n     int n; n     scanf( %d , &n); n     vector<int> a, b; n     a.push_back(1); n     for (int i = 2; i <= n; i++) b.push_back(i); n     int t = que(a, b); n     int st = 1, ed = n; n     while (st + 1 < ed) { n       int md = st + ed >> 1; n       b.clear(); n       for (int i = 2; i <= md; i++) b.push_back(i); n       if (que(a, b) == t) n         ed = md; n       else n         st = md; n     } n     a.clear(); n     b.clear(); n     a.push_back(ed); n     for (int i = 1; i <= n; i++) n       if (ed != i) b.push_back(i); n     t = que(a, b); n     printf( -1 %d n , t); n     fflush(stdout); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[100010], ar[100010]; n int main() { n   int n, track = -100002, i; n   cin >> n; n   for (i = 1; i <= n; i++) { n     cin >> a[i]; n     ar[a[i]] = ar[a[i] - 1] + 1; n     track = max(ar[a[i]], track); n   } n   cout << n - track << endl; n } n


#include <bits/stdc++.h> n using namespace std; n bool compare(pair<int, int> a, pair<int, int> b) { return a.first < b.first; } n int main() { n   float r1, r2, c1, c2, d1, d2; n   cin >> r1 >> r2 >> c1 >> c2 >> d1 >> d2; n   float a = c1 / 2 + (d1 + r1 - d2 - r2) / 4; n   float c = c1 / 2 - (d1 + r1 - d2 - r2) / 4; n   float b = c2 / 2 + (r1 + d2 - r2 - d1) / 4; n   float d = c2 / 2 - (r1 + d2 - r2 - d1) / 4; n   if (a > 9 || a < 1) { n     cout << -1; n     return 0; n   } n   if (b > 9 || b < 1) { n     cout << -1; n     return 0; n   } n   if (c > 9 || c < 1) { n     cout << -1; n     return 0; n   } n   if (d > 9 || d < 1) { n     cout << -1; n     return 0; n   } n   if (a + b != r1) { n     cout << -1; n     return 0; n   } n   if (d + c != r2) { n     cout << -1; n     return 0; n   } n   if (a + c != c1) { n     cout << -1; n     return 0; n   } n   if (d + b != c2) { n     cout << -1; n     return 0; n   } n   if (a + d != d1) { n     cout << -1; n     return 0; n   } n   if (c + b != d2) { n     cout << -1; n     return 0; n   } n   if (a == b || a == c || a == d || b == c || b == d || c == d) { n     cout << -1; n     return 0; n   } n   cout << (int)a <<     << (int)b << endl << (int)c <<     << (int)d; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000007; n long long Fib[100]; n long long Path[100]; n pair<long long, long long> get(long long N, long long X, bool f) { n   assert(Fib[N] >= X); n   if (N <= 0) { n     return {0, 0}; n   } n   if (N == 1) { n     if (X == 1) { n       return {0, 1}; n     } else { n       return {1, 0}; n     } n   } n   pair<long long, long long> res = {}; n   if (X <= Fib[N - 1]) { n     res = get(N - 1, X, true); n     res.second = min(res.first, res.second) + 1ll + Path[N - 2]; n     if (f) { n       res.second = min(res.second, res.first + 2ll); n       res.first = min(res.first, res.second + 2ll); n     } n   } else { n     res = get(N - 2, X - Fib[N - 1], false); n     res.first++; n     if (f) { n       res.second = min(res.second, res.first + 2ll); n       res.first = min(res.first, res.second + 2ll); n     } n   } n   return res; n } n long long solve(long long N, long long X, long long Y) { n   int f = 0; n   if (N > 90) { n     N = 90; n     f = 1; n   } n   assert(Fib[N] >= X && Fib[N] >= Y); n   if (X > Y) swap(X, Y); n   while (true) { n     if (Y <= Fib[N - 1]) { n       f = 1; n       N--; n     } else if (X > Fib[N - 1]) { n       if (f) { n         f++; n       } n       X -= Fib[N - 1]; n       Y -= Fib[N - 1]; n       N -= 2; n     } else { n       break; n     } n   } n   if (N == 1) { n     return 1; n   } n   auto res1 = get(N - 1, X, true); n   auto res2 = get(N - 2, Y - Fib[N - 1], false); n   long long res = res2.first + 1ll + min(res1.first, res1.second); n   if (f) { n     res = min(res, res2.second + 2ll + res1.first + f - 1ll); n   } n   return res; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   Fib[0] = 1; n   Path[0] = 0; n   Fib[1] = 2; n   Path[1] = 1; n   for (int i = 2; i < 100; i++) { n     Fib[i] = Fib[i - 1] + Fib[i - 2]; n     Path[i] = Path[i - 2] + 1; n   } n   long long T, N, X, Y; n   cin >> T >> N; n   while (T--) { n     cin >> X >> Y; n     cout << solve(N, X, Y) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n namespace { n const double PI = acos(-1.0); n } n struct Data { n   int n; n   int c; n   vector<int> t; n   Data() : c(0) {} n   bool operator<(const Data& d) const { return c < d.c; } n }; n int main() { n   int n; n   while (cin >> n) { n     n *= 2; n     Data d[101]; n     for (int i = 0; i < n; ++i) { n       int x; n       cin >> x; n       d[x].n = x; n       ++d[x].c; n       d[x].t.push_back(i); n     } n     sort(d, d + 101); n     bool f = 0; n     vector<int> ans[2]; n     set<int> s[2]; n     int z[101] = {0}; n     for (int i = 0; i < 101; ++i) { n       for (int j = 0; j < d[i].c; ++j) { n         ans[f].push_back(d[i].n); n         s[f].insert(d[i].n); n         z[d[i].t[j]] = f; n         f = !f; n       } n     } n     cout << s[0].size() * s[1].size() << endl; n     for (int i = 0; i < n; ++i) { n       if (i) cout <<    ; n       cout << z[i] + 1; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long max(long long a, long long b) { return (a > b) ? a : b; } n inline long long min(long long a, long long b) { return (a < b) ? a : b; } n inline long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n vector<vector<long long> > graph; n vector<long long> color, initial; n long long cnt = 0; n void bfs(long long source, long long n, long long colour) { n   queue<long long> q; n   q.push(source); n   bool visited[n + 1]; n   fill(visited, visited + n + 1, false); n   visited[source] = true; n   while (!q.empty()) { n     long long u = q.front(); n     q.pop(); n     for (auto c : graph[u]) { n       if (visited[c] == false) { n         q.push(c); n         visited[c] = true; n         initial[c] = colour; n       } n     } n   } n } n int main() { n   ; n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long n; n   cin >> n; n   graph.resize(n + 1); n   for (long long i = 2; i <= n; i++) { n     long long a; n     cin >> a; n     graph[a].push_back(i); n   } n   initial.resize(n + 1, -1); n   color.resize(n + 1, 0); n   for (long long i = 1; i <= n; i++) { n     cin >> color[i]; n     if (initial[i] == color[i]) continue; n     cnt++; n     initial[i] = color[i]; n     bfs(i, n, color[i]); n   } n   cout << cnt; n   cout <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n long long int prime[1000010] = {0}; n vector<long long int> pr; n void sieve() { n   prime[0] = 1; n   prime[1] = 1; n   for (long long int i = 2; i <= sqrt(1000010); i++) { n     if (prime[i] == 0) { n       for (long long int j = i * i; j < 1000010; j += i) { n         prime[j] = 1; n       } n     } n   } n   for (long long int i = 2; i < 1000010; i++) { n     if (!prime[i]) pr.push_back(i); n   } n   return; n } n double pi = 3.141592653589; n long long int a[200100]; n long long int pre[200100] = {0}; n signed main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long int n, w; n   cin >> n >> w; n   for (long long int i = 1; i <= n; i += 1) { n     cin >> a[i]; n     pre[i] = pre[i - 1] + a[i]; n   } n   long long int mi = 0, ma = 0; n   for (long long int i = 1; i <= n; i += 1) { n     mi = min(mi, pre[i]); n     ma = max(ma, pre[i]); n   } n   long long int ans = (w - ma) + (mi + 1); n   if (ans > w + 1 || ans < 0) n     cout << 0 << endl; n   else n     cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   long long t; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     long long arr[n]; n     for (long long i = 0; i < n; i++) cin >> arr[i]; n     sort(arr, arr + n); n     bool f = 1; n     for (long long i = 1; i < n; i++) { n       if (arr[i] - arr[i - 1] > 1) f = 0; n     } n     if (f) n       cout <<  YES n ; n     else n       cout <<  NO n ; n   } n   return 0; n } n