#include <bits/stdc++.h> n using namespace std; n const int maxn = 510; n char g[maxn][maxn]; n int col[maxn][maxn], v[maxn * maxn], b[maxn * maxn], cnt, n, k, sum; n int dx[] = {0, 0, 1, -1}; n int dy[] = {1, -1, 0, 0}; n void dfs(int x, int y, int c) { n   col[x][y] = c; n   ++cnt; n   for (int i = 0; i < 4; ++i) { n     int tx = x + dx[i], ty = y + dy[i]; n     if (tx < 0 || ty < 0 || tx >= n || ty >= n || g[tx][ty] ==  X  || n         col[tx][ty]) n       continue; n     dfs(tx, ty, c); n   } n } n void add(int x, int y) { n   if (col[x][y] && ++v[col[x][y]] == 1) sum += b[col[x][y]]; n } n void dec(int x, int y) { n   if (col[x][y] && --v[col[x][y]] == 0) sum -= b[col[x][y]]; n } n int main() { n   scanf( %d %d , &n, &k); n   for (int i = 0; i < n; ++i) scanf( %s , g[i]); n   int c = 1; n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < n; ++j) n       if (g[i][j] ==  .  && !col[i][j]) { n         cnt = 0; n         dfs(i, j, c); n         b[c++] = cnt; n       } n   } n   int ans = 0; n   for (int i = 0; i + k <= n; ++i) { n     memset(v, 0, sizeof v); n     sum = 0; n     for (int x = 0; x < k; ++x) { n       for (int y = 0; y < k; ++y) { n         add(x + i, y); n         if (!col[x + i][y]) ++sum; n       } n       if (i + k < n) add(i + k, x); n       if (k < n) add(x + i, k); n       if (i) add(i - 1, x); n     } n     ans = max(ans, sum); n     for (int j = 1; j + k <= n; ++j) { n       if (i) { n         if (j - 1 >= 0) dec(i - 1, j - 1); n         add(i - 1, j + k - 1); n       } n       if (i + k < n) { n         if (j - 1 >= 0) dec(i + k, j - 1); n         add(i + k, j + k - 1); n       } n       if (j - 1 >= 0) n         for (int t = 0; t < k; ++t) n           if (!col[i + t][j - 1]) --sum; n       if (j - 2 >= 0) n         for (int t = 0; t < k; ++t) dec(i + t, j - 2); n       if (j + k - 1 < n) n         for (int t = 0; t < k; ++t) n           if (!col[i + t][j + k - 1]) ++sum; n       if (j + k <= n) n         for (int t = 0; t < k; ++t) add(i + t, j + k); n       ans = max(ans, sum); n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1000100; n int N; n long long K; n long long arr[MAXN], anew[MAXN], tcur[MAXN], tnew[MAXN]; n int main() { n   scanf( %d %I64d , &N, &K); n   arr[0] = 0; n   for (int i = 1; i < N; i++) { n     int p; n     long long w; n     scanf( %d %I64d , &p, &w); n     p--; n     arr[i] = arr[p] ^ w; n   } n   sort(arr, arr + N); n   for (int i = 0; i < N; i++) anew[i] = arr[i]; n   long long ans = 0; n   for (int i = 61; i >= 0; i--) { n     for (int j = 0; j < N; j++) { n       tcur[j] = (arr[j] >> i); n       tnew[j] = (anew[j] >> i); n     } n     long long pp = 0; n     int lloc = 0, rloc = 0; n     while (lloc < N) { n       long long cv = tcur[lloc]; n       while (rloc < N && tnew[rloc] < cv) rloc++; n       long long nl = 0, nr = 0; n       while (lloc < N && tcur[lloc] == cv) { n         lloc++; n         nl++; n       } n       while (rloc < N && tnew[rloc] == cv) { n         rloc++; n         nr++; n       } n       pp += nl * nr; n     } n     if (pp < K) { n       ans += (1LL << i); n       K -= pp; n       for (int j = 0; j < N; j++) anew[j] ^= (1LL << i); n       for (int j = 0; j < N - 1; j++) { n         if (anew[j] > anew[j + 1]) { n           int cj = j, ej = j + 1; n           while (cj && anew[cj - 1] > anew[j + 1]) cj--; n           while (ej < N - 1 && anew[ej + 1] < anew[j]) ej++; n           reverse(anew + cj, anew + j + 1); n           reverse(anew + j + 1, anew + ej + 1); n           reverse(anew + cj, anew + ej + 1); n         } n       } n     } n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> by; n vector<int> sl; n int main() { n   long long int a, b, c, d, m, n, ck = 0, ans, s, r; n   cin >> n >> m >> r; n   for (int i = 0; i < n; i++) { n     cin >> s; n     by.push_back(s); n   } n   sort(by.begin(), by.end()); n   for (int j = 0; j < m; j++) { n     cin >> b; n     sl.push_back(b); n   } n   sort(sl.begin(), sl.end(), greater<int>()); n   long long int buy = r / by[0]; n   long long int rem = r % by[0]; n   long long int sell = buy * sl[0]; n   ans = sell + rem; n   if (ans > r) { n     cout << ans << endl; n   } else { n     cout << r << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 10; n long long int M = 1e9 + 7; n long long int MOD = 998244353; n const double PI = 3.141592653589793238460; n long long int power(long long int a, long long int b) { n   long long int res = 1; n   if (a == 0) return 0; n   if (a == 1) return 1; n   for (; b > 0; b >>= 1) { n     if (b & 1) { n       res = (res * a); n       if (res > M) res %= M; n     } n     a = (a * a); n     if (a > M) a = a % M; n   } n   return res; n } n int main() { n   long long int maxi = -1e18; n   long long int n; n   cin >> n; n   long long int a[n], b[n]; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0; i < n; i++) cin >> b[i]; n   for (int i = 0; i < n; i++) { n     long long int x = a[i]; n     long long int y = b[i]; n     for (int j = i; j < n; j++) { n       x |= (a[j]); n       y |= (b[j]); n       maxi = max(maxi, x + y); n     } n   } n   cout << maxi; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int a[n]; n   int arr[n][n]; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) cin >> arr[i][j]; n   } n   a[0] = 0; n   for (int j = 1; j < n; j++) { n     a[0] = a[0] | arr[0][j]; n     a[j] = arr[0][j]; n   } n   for (int i = 1; i < n; i++) { n     for (int j = i + 1; j < n; j++) { n       a[i] = a[i] | arr[i][j]; n       a[j] = arr[i][j] | a[j]; n     } n   } n   for (int i = 0; i < n; i++) cout << a[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long power(long long a, long long p, long long mod) { n   long long ret = 1; n   while (p) { n     if (p & 1) ret = (ret * a) % mod; n     a = (a * a) % mod; n     p /= 2; n   } n   return ret; n } n long long DP[1000]; n int A[1000]; n int main() { n   int n; n   cin >> n; n   for (__typeof((__typeof(n))0) i((__typeof(n))0); i < n; i++) { n     cin >> A[i]; n     A[i]--; n   } n   for (int i = 0; i < n; i++) { n     DP[i] = 2; n     for (int j = A[i]; j < i; j++) DP[i] = (DP[i] + DP[j]) % 1000000007; n   } n   long long ans = 0; n   for (int i = 0; i < n; i++) ans = (ans + DP[i]) % 1000000007; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1511, M = 3010100, inf = 0x7fffffff; n const long long llinf = 0x7fffffff7fffffffll; n long long x[N], y[N], r[N]; n long long c[N]; n double dist(int i, int j) { n   return sqrtl((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j])); n } n double area(int i) { n   return 3.14159265358979323846264338327950288 * r[i] * r[i]; n } n int main(int argc, const char* argv[]) { n   int n; n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     cin >> x[i] >> y[i] >> r[i]; n   } n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= n; ++j) { n       if (r[i] < r[j] && dist(i, j) <= r[j]) { n         ++c[i]; n       } n     } n   } n   double sum = 0; n   for (int i = 1; i <= n; ++i) { n     if (c[i] == 0) { n       sum += area(i); n     } else if (c[i] % 2 == 1) { n       sum += area(i); n     } else { n       sum -= area(i); n     } n   } n   printf( %.10f , sum); n   return 0; n } n


#include <bits/stdc++.h> n // #include <atcoder/all> n  n //tabaicho  see https://boostjp.github.io/tips/multiprec-int.html    n // #include <boost/multiprecision/cpp_int.hpp> n  n // using namespace boost::multiprecision; n  n // cpp_int n // int128_t n // int256_t n // int512_t n // int1024_t n  n // uint128_t n // uint256_t n // uint512_t n // uint1024_t n  n  n  n  #define int long long n  #define inf  1000000007 n // #define inf  998244353 n  n  #define pa pair<int,int> n  #define ll long long n  #define PI 3.14159265358979323846 n  #define  mp make_pair n  #define  pb push_back n  #define EPS (1e-8) n   n       using namespace std; n                                            n  int dx[8]={0,1,0,-1,1,1,-1,-1}; n  int dy[8]={1,0,-1,0,-1,1,1,-1}; n                                              n class pa3{ n  public: n  int x; n  int y,z; n  pa3(int x=0,int y=0,int z=0):x(x),y(y),z(z) {} n  bool operator < (const pa3 &p) const{ n   if(x!=p.x) return x<p.x; n   if(y!=p.y) return y<p.y; n    return z<p.z; n   //return x != p.x ? x<p.x: y<p.y; n  } n  bool operator > (const pa3 &p) const{ n   if(x!=p.x) return x>p.x; n   if(y!=p.y) return y>p.y; n    return z>p.z; n   //return x != p.x ? x<p.x: y<p.y; n  } n  bool operator == (const pa3 &p) const{ n   return x==p.x && y==p.y && z==p.z; n  } n   bool operator != (const pa3 &p) const{ n    return !( x==p.x && y==p.y && z==p.z); n  } n  n }; n  n class pa4{ n  public: n  int x; n  int y,z,w; n  pa4(int x=0,int y=0,int z=0,int w=0):x(x),y(y),z(z),w(w) {} n  bool operator < (const pa4 &p) const{ n   if(x!=p.x) return x<p.x; n   if(y!=p.y) return y<p.y; n   if(z!=p.z)return z<p.z; n   return w<p.w; n   //return x != p.x ? x<p.x: y<p.y; n  } n  bool operator > (const pa4 &p) const{ n   if(x!=p.x) return x>p.x; n   if(y!=p.y) return y>p.y; n   if(z!=p.z)return z>p.z; n   return w>p.w; n   //return x != p.x ? x<p.x: y<p.y; n  } n  bool operator == (const pa4 &p) const{ n   return x==p.x && y==p.y && z==p.z &&w==p.w; n  } n    n  n }; n class pa2{ n  public: n  int x,y; n  pa2(int x=0,int y=0):x(x),y(y) {} n  pa2 operator + (pa2 p) {return pa2(x+p.x,y+p.y);} n  pa2 operator - (pa2 p) {return pa2(x-p.x,y-p.y);} n  bool operator < (const pa2 &p) const{ n   return y != p.y ? y<p.y: x<p.x; n  } n  bool operator > (const pa2 &p) const{ n   return x != p.x ? x<p.x: y<p.y; n  } n  bool operator == (const pa2 &p) const{ n   return abs(x-p.x)==0 && abs(y-p.y)==0; n  } n  bool operator != (const pa2 &p) const{ n   return !(abs(x-p.x)==0 && abs(y-p.y)==0); n  } n    n  n }; n  n  n  n string itos( int i ) { n  ostringstream s ; n  s << i ; n  return s.str() ; n } n   n int Gcd(int v,int b){ n  if(v==0) return b; n  if(b==0) return v; n  if(v>b) return Gcd(b,v); n  if(v==b) return b; n  if(b%v==0) return v; n  return Gcd(v,b%v); n } n  n  n  int mod; n int extgcd(int a, int b, int &x, int &y) { n     if (b == 0) { n         x = 1; n         y = 0; n         return a; n     } n     int d = extgcd(b, a%b, y, x); n     y -= a/b * x; n     return d; n } n pa operator+(const pa & l,const pa & r) {    n     return {l.first+r.first,l.second+r.second};                                     n }     n pa operator-(const pa & l,const pa & r) {    n     return {l.first-r.first,l.second-r.second};                                     n }   n  n ostream& operator<<(ostream& os, const vector<int>& VEC){ n  for(auto v:VEC)os<<v<<   ; n     return os; n } n  n ostream& operator<<(ostream& os, const pa& PAI){ n  os<<PAI.first<<  :  <<PAI.second; n     return os; n } n  n ostream& operator<<(ostream& os, const pa3& PAI){ n  os<<PAI.x<<  :  <<PAI.y<<  :  <<PAI.z; n     return os; n } n  n ostream& operator<<(ostream& os, const pa4& PAI){ n  os<<PAI.x<<  :  <<PAI.y<<  :  <<PAI.z<<  :  <<PAI.w; n     return os; n } n  n ostream& operator<<(ostream& os, const vector<pa>& VEC){ n  for(auto v:VEC)os<<v<<   ; n     return os; n } n  n  n int beki(int wa,int rr,int warukazu){ n  if(rr==0) return 1%warukazu; n  if(rr==1) return wa%warukazu; n  wa%=warukazu; n  if(rr%2==1) return ((ll)beki(wa,rr-1,warukazu)*(ll)wa)%warukazu; n  ll zx=beki(wa,rr/2,warukazu); n  return (zx*zx)%warukazu; n } n  n                n int pr[1]; n int inv[1]; n  n  n  n int comb(int nn,int rr){ n  if(rr<0 || rr>nn || nn<0) return 0; n  int r=pr[nn]*inv[rr]; n  r%=mod; n  r*=inv[nn-rr]; n  r%=mod; n  return r; n } n  n void gya(int ert){ n  pr[0]=1; n  for(int i=1;i<=ert;i++){ n   pr[i]=((ll)pr[i-1]*i)%mod; n  } n   inv[ert]=beki(pr[ert],mod-2,mod); n  for(int i=ert-1;i>=0;i--){ n   inv[i]=(ll)inv[i+1]*(i+1)%mod; n  } n } n  n  n                  n  n  n                  n               //   cin.tie(0); n       // ios::sync_with_stdio(false); n        //priority_queue<pa3,vector<pa3>,greater<pa3>> pq;             n                  //sort(ve.begin(),ve.end(),greater<int>()); n  //   mt19937(clock_per_sec); n   //  mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()) ; n  n  n  n  n  n void solve(){ n int n; n  cin>>n; n  vector<int>dp(n,0); n   n   n  vector<int>id(n),s(n); n  //int xn=0; n  for(auto &v:id){ n   cin>>v; n   v--; n  // pos[v].pb(xn); n  // xn++; n  } n  for(auto &v:s)cin>>v; n   n  int ans=0; n  for(int i=n-1;i>=0;i--){ n   int pos=0; n   for(int j=0;j<i;j++){ n    if(id[i]!=id[j]){ n     int g=dp[j]+abs(s[i]-s[j]); n     int f=max(dp[i],pos)+abs(s[i]-s[j]); n      n     dp[j]=max(dp[j],f); n     pos=max(pos,g); n     ans=max(ans,g); n     ans=max(ans,f); n    } n     n   } n  } n  cout<<ans<<endl; n  n } n  n  n  n signed main(){ n  mod=inf; n  cin.tie(0); n  ios::sync_with_stdio(false); n int n=1; n  cin>>n; n  n  for(int i=0;i<n;i++)solve(); n   n  return 0; n  } n   n


#include <bits/stdc++.h> n using namespace std; n vector<string> prices; n enum State { name, price }; n double to_num(string s) { n   double n = 0; n   int dots = 0, dot = 0; n   bool frac = false; n   for (char c : s) n     if (c ==  . ) ++dots; n   if (s[s.size() - 3] ==  . ) frac = true; n   for (int i = 0; i < s.size(); ++i) { n     if (s[i] >=  0  && s[i] <=  9 ) { n       n *= 10; n       n += s[i] -  0 ; n     } else if (s[i] ==  . ) n       ++dot; n     if (dot == dots && frac) { n       n += double(s[i + 1] -  0 ) / 10.; n       n += double(s[i + 2] -  0 ) / 100.; n       break; n     } n   } n   return n; n } n string to_price(double n) { n   string s = to_string(n); n   string fraction; n   int dot = 0; n   for (int i = 0; i < s.size(); ++i) n     if (s[i] ==  . ) dot = i; n   if (s[dot + 1] !=  0  || s[dot + 2] !=  0 ) { n     fraction.push_back( . ); n     fraction.push_back(s[dot + 1]); n     fraction.push_back(s[dot + 2]); n   } n   string c = s.substr(0, dot); n   s = c; n   string integer = to_string(int(n)); n   string res; n   for (int i = integer.size() - 1, k = 0; i >= 0; --i) { n     ++k; n     res.push_back(integer[i]); n     if (k % 3 == 0 && i != 0) res.push_back( . ); n   } n   reverse(res.begin(), res.end()); n   res += fraction; n   return res; n } n int main() { n   string input; n   getline(cin, input); n   string s; n   State state = name; n   for (char c : input) { n     if (isalpha(c) && state == name) n       continue; n     else if (!isalpha(c) && state == name) { n       state = price; n       s.push_back(c); n     } else if (!isalpha(c) && state == price) n       s.push_back(c); n     else if (isalpha(c) && state == price) { n       state = name; n       prices.push_back(s); n       s.clear(); n     } n   } n   prices.push_back(s); n   double sum = 0; n   for (int i = 0; i < prices.size(); ++i) sum += to_num(prices[i]); n   cout << to_price(sum); n } n


#include <bits/stdc++.h> n const int fin = 0, maxn = 400000, maxm = maxn << 1; n int n, m, cent, tot; n int fs[maxn], e[maxm], next[maxm]; n int bel[maxn], pos[maxn], len[maxn], left[maxn]; n int c[maxn], deg[maxn], b[maxn]; n bool v[maxn]; n struct rec { n   int x, y; n } g[maxn]; n int sum(int *a, int n, int x) { n   int s = 0; n   for (; x; x -= ((x) & (-(x)))) s += a[x]; n   return s; n } n void upd(int *a, int n, int x, int w) { n   for (; x <= n; x += ((x) & (-(x)))) a[x] += w; n } n inline void addedge(int x, int y) { n   e[++m] = y; n   next[m] = fs[x]; n   fs[x] = m; n   e[++m] = x; n   next[m] = fs[y]; n   fs[y] = m; n } n void init() { n   int i; n   scanf( %d , &n); n   m = 0; n   for (i = 1; i < n; ++i) { n     scanf( %d%d , &g[i].x, &g[i].y); n     addedge(g[i].x, g[i].y); n     deg[g[i].x]++; n     deg[g[i].y]++; n   } n   cent = 0; n   for (i = 1; i <= n; ++i) n     if (deg[i] > 2) { n       cent = i; n       break; n     } n   if (!cent) cent = 1; n } n void build() { n   int i, j, p, k, t; n   i = fs[cent]; n   tot = 0; n   v[cent] = true; n   bel[cent] = 0; n   for (j = 1; j <= deg[cent]; ++j) { n     p = e[i]; n     bel[p] = j; n     v[p] = true; n     b[++tot] = p; n     pos[p] = 1; n     left[j] = tot; n     while (next[fs[p]]) { n       if (v[e[fs[p]]]) n         t = e[next[fs[p]]]; n       else n         t = e[fs[p]]; n       v[t] = true; n       b[++tot] = t; n       bel[t] = j; n       pos[t] = pos[p] + 1; n       p = t; n     } n     len[j] = tot - left[j] + 1; n     i = next[i]; n     for (k = 1; k <= len[j]; ++k) upd(c + left[j] - 1, len[j], k, 1); n   } n } n void solve() { n   int que, x, y, j, t, type, p, k; n   scanf( %d , &que); n   while (que--) { n     scanf( %d , &type); n     if (type == 1) { n       scanf( %d , &p); n       x = g[p].x; n       y = g[p].y; n       if ((!bel[x]) || (!bel[y])) { n         j = bel[y] + bel[x]; n         upd(c + left[j] - 1, len[j], 1, 1); n       } else { n         j = bel[x]; n         if (pos[x] > pos[y]) n           k = pos[x]; n         else n           k = pos[y]; n         upd(c + left[j] - 1, len[j], k, 1); n       } n     } else if (type == 2) { n       scanf( %d , &p); n       x = g[p].x; n       y = g[p].y; n       if ((!bel[x]) || (!bel[y])) { n         j = bel[y] + bel[x]; n         upd(c + left[j] - 1, len[j], 1, -1); n       } else { n         j = bel[x]; n         if (pos[x] > pos[y]) n           k = pos[x]; n         else n           k = pos[y]; n         upd(c + left[j] - 1, len[j], k, -1); n       } n     } else { n       scanf( %d%d , &x, &y); n       if (x == y) n         printf( 0 n ); n       else { n         if ((!bel[x]) || (!bel[y])) { n           j = bel[x] + bel[y]; n           if (!bel[x]) n             k = pos[y]; n           else n             k = pos[x]; n           t = sum(c + left[j] - 1, len[j], k); n           if (t == k) n             printf( %d n , t); n           else n             printf( -1 n ); n         } else if (bel[x] == bel[y]) { n           j = bel[x]; n           if (pos[x] > pos[y]) { n             t = x; n             x = y; n             y = t; n           } n           t = sum(c + left[j] - 1, len[j], pos[y]) - n               sum(c + left[j] - 1, len[j], pos[x]); n           if (t == pos[y] - pos[x]) n             printf( %d n , t); n           else n             printf( -1 n ); n         } else { n           j = bel[x]; n           t = sum(c + left[j] - 1, len[j], pos[x]); n           j = bel[y]; n           t += sum(c + left[j] - 1, len[j], pos[y]); n           if (t == pos[x] + pos[y]) n             printf( %d n , t); n           else n             printf( -1 n ); n         } n       } n     } n   } n } n int main() { n   if (fin) { n     freopen( t.in ,  r , stdin); n     freopen( t.out ,  w , stdout); n   } n   init(); n   build(); n   solve(); n   if (fin) { n     fclose(stdin); n     fclose(stdout); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct dot { n   int value, i, j; n }; n bool operator<(dot a, dot b) { return a.value < b.value; } n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     int n, m; n     scanf( %d %d , &n, &m); n     int num[n][m]; n     priority_queue<dot> pq; n     for (int i = 0; i < n; ++i) { n       for (int j = 0; j < m; ++j) { n         scanf( %d , &num[i][j]); n         pq.push({num[i][j], i, j}); n       } n     } n     dot d[n]; n     for (int i = 0; i < n; ++i) { n       d[i] = pq.top(); n       pq.pop(); n     } n     if (n == 4) { n       bool did = false; n       dot dsave; n       if (d[0].j == d[1].j && d[2].j == d[3].j && d[0].j != d[2].j) { n         bool b1 = abs(d[0].i - d[1].i) == 2; n         bool b2 = abs(d[2].i - d[3].i) == 2; n         if (b1 ^ b2) { n           dsave = d[3]; n           d[3] = pq.top(); n           did = true; n         } n       } else if (d[0].j == d[2].j && d[1].j == d[3].j && d[0].j != d[1].j) { n         bool b1 = abs(d[0].i - d[2].i) == 2; n         bool b2 = abs(d[1].i - d[3].i) == 2; n         if (b1 ^ b2) { n           dsave = d[3]; n           d[3] = pq.top(); n           did = true; n         } n       } else if (d[0].j == d[3].j && d[1].j == d[2].j && d[0].j != d[1].j) { n         bool b1 = abs(d[0].i - d[3].i) == 2; n         bool b2 = abs(d[1].i - d[2].i) == 2; n         if (b1 ^ b2) { n           dsave = d[3]; n           d[3] = pq.top(); n           did = true; n         } n       } n       if (did) { n         bool done = true; n         if (d[0].j == d[1].j && d[2].j == d[3].j && d[0].j != d[2].j) { n           bool b1 = abs(d[0].i - d[1].i) == 2; n           bool b2 = abs(d[2].i - d[3].i) == 2; n           if (b1 ^ b2) done = false; n         } else if (d[0].j == d[2].j && d[1].j == d[3].j && d[0].j != d[1].j) { n           bool b1 = abs(d[0].i - d[2].i) == 2; n           bool b2 = abs(d[1].i - d[3].i) == 2; n           if (b1 ^ b2) done = false; n         } else if (d[0].j == d[3].j && d[1].j == d[2].j && d[0].j != d[1].j) { n           bool b1 = abs(d[0].i - d[3].i) == 2; n           bool b2 = abs(d[1].i - d[2].i) == 2; n           if (b1 ^ b2) done = false; n         } n         if (done) { n         } else { n           d[2] = dsave; n         } n       } n     } n     int ret = 0; n     for (int i = 0; i < n; ++i) { n       ret += d[i].value; n     } n     printf( %d n , ret); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, sum, ans; n int cnt[1000010], pre[1000010]; n char a[1000010], b[1000010]; n int main() { n   scanf( %s%s , a + 1, b + 1); n   n = strlen(a + 1); n   m = strlen(b + 1); n   for (int i = 1; i < n; i++) n     if (a[i] != a[i + 1]) cnt[i] = 1; n   for (int i = 1; i <= n; i++) pre[i] = pre[i - 1] + cnt[i]; n   for (int i = 1; i <= m; i++) { n     if (a[i] != b[i]) sum++; n   } n   if (sum % 2 == 0) ans = 1; n   for (int i = m + 1; i <= n; i++) { n     sum += pre[i - 1] - pre[i - m - 1]; n     if (sum % 2 == 0) ans++; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, i, j, sum, count; n   cin >> n; n   int a[n][n]; n   int row[n], col[n]; n   for (i = 0; i < n; i++) { n     sum = 0; n     for (j = 0; j < n; j++) { n       cin >> a[i][j]; n       sum += a[i][j]; n     } n     row[i] = sum; n   } n   for (j = 0; j < n; j++) { n     sum = 0; n     for (i = 0; i < n; i++) { n       sum += a[i][j]; n     } n     col[j] = sum; n   } n   count = 0; n   for (i = 0; i < n; i++) { n     for (j = 0; j < n; j++) { n       if (col[i] > row[j]) count++; n     } n   } n   cout << count << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n const int mo = 1000000007; n inline void gn(long long &x) { n   int sg = 1; n   char c; n   while (((c = getchar()) <  0  || c >  9 ) && c !=  - ) n     ; n   c ==  -  ? (sg = -1, x = 0) : (x = c -  0 ); n   while ((c = getchar()) >=  0  && c <=  9 ) x = x * 10 + c -  0 ; n   x *= sg; n } n inline void gn(int &x) { n   long long t; n   gn(t); n   x = t; n } n inline void gn(unsigned long long &x) { n   long long t; n   gn(t); n   x = t; n } n inline void gn(double &x) { n   double t; n   scanf( %lf , &t); n   x = t; n } n inline void gn(long double &x) { n   double t; n   scanf( %lf , &t); n   x = t; n } n template <class T1, class T2> n inline void gn(T1 &r, T2 &s) { n   gn(r), gn(s); n } n template <class T1, class T2, class T3> n inline void gn(T1 &r, T2 &s, T3 &t) { n   gn(r), gn(s), gn(t); n } n template <class T1, class T2, class T3, class T4> n inline void gn(T1 &r, T2 &s, T3 &t, T4 &u) { n   gn(r), gn(s), gn(t), gn(u); n } n inline void gs(char *s) { scanf( %s , s); } n inline void gc(char &c) { n   while ((c = getchar()) > 126 || c < 33) n     ; n } n inline void pc(char c) { putchar(c); } n const int DX[] = {1, 0, -1, 0}, DY[] = {0, 1, 0, -1}; n long long powmod(long long a, long long b) { n   long long res = 1; n   a %= mo; n   for (; b; b >>= 1) { n     if (b & 1) res = res * a % mo; n     a = a * a % mo; n   } n   return res; n } n long long powmod(long long a, long long b, long long mo) { n   long long res = 1; n   a %= mo; n   for (; b; b >>= 1) { n     if (b & 1) res = res * a % mo; n     a = a * a % mo; n   } n   return res; n } n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n const int N = 5055, M = 111111; n int l, m, n, t, C, x, y, prex; n vector<int> a[N]; n int dis[N][N]; n void bfs(int k) { n   for (int i = (1); i <= (n); ++i) dis[k][i] = inf; n   dis[k][k] = 0; n   queue<int> Q; n   Q.push(k); n   while (!Q.empty()) { n     int p = Q.front(); n     Q.pop(); n     for (auto i : a[p]) { n       if (dis[k][i] == inf) { n         dis[k][i] = dis[k][p] + 1; n         Q.push(i); n       } n     } n   } n } n int p[9]; n void update() { n   printf( %d %d %d n , p[0], p[1], p[2]); n   fflush(stdout); n   if (p[0] == x || p[1] == x || p[2] == x) exit(0); n   prex = x; n   scanf( %d , &x); n   if (p[0] == x || p[1] == x || p[2] == x) exit(0); n } n void go(int &x, int y) { n   for (auto i : a[x]) n     if (dis[i][y] == dis[x][y] - 1) { n       x = i; n       break; n     } n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = (1); i <= (m); ++i) n     scanf( %d%d , &x, &y), a[x].push_back(y), a[y].push_back(x); n   for (int i = (1); i <= (n); ++i) bfs(i); n   p[0] = p[1] = p[2] = 1; n   x = -1; n   while (1) { n     update(); n     int q[9]; n     int ans = inf; n     for (int i = (0); i <= (((int)(a[x]).size()) - 1); ++i) n       for (int j = (0); j <= (((int)(a[x]).size()) - 1); ++j) n         for (int k = (0); k <= (((int)(a[x]).size()) - 1); ++k) { n           int res = n               dis[p[0]][a[x][i]] + dis[p[1]][a[x][j]] + dis[p[2]][a[x][k]]; n           if (i != j) res -= n; n           if (j != k) res -= n; n           if (i != k) res -= n; n           if (res < ans) n             ans = res, q[0] = a[x][i], q[1] = a[x][j], q[2] = a[x][k]; n         } n     for (int i = (0); i <= (2); ++i) n       if (dis[p[i]][x] == 1) q[i] = x; n     for (int i = (0); i <= (2); ++i) go(p[i], q[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, q; n pair<int, int> a[400004]; n long long ans[200004], d[400004], sum; n int fa[400004], l[400004], r[400004], s[400004]; n vector<pair<int, int> > v, ask; n int F(int x) { return fa[x] == x ? x : fa[x] = F(fa[x]); } n long long got(int v) { return d[r[v]] - d[r[v] - s[v]]; } n int main() { n   scanf( %d%d%d , &n, &m, &q); n   for (int i = 1; i <= n + m; i++) scanf( %d , &a[i].first), a[i].second = i; n   sort(a + 1, a + n + m + 1); n   for (int i = 1; i <= n + m; i++) { n     d[i] = d[i - 1] + a[i].first; n     fa[i] = i, s[i] = (a[i].second <= n), l[i] = r[i] = i; n     if (s[i] == 1) sum += a[i].first; n     if (i > 1) v.push_back({a[i].first - a[i - 1].first, i}); n   } n   for (int i = 1; i <= q; i++) { n     int k; n     scanf( %d , &k); n     ask.push_back({k, i}); n   } n   sort(v.begin(), v.end()); n   sort(ask.begin(), ask.end()); n   int it = 0; n   for (auto qr : ask) { n     while (it < v.size() && v[it].first <= qr.first) { n       int p = v[it].second; n       int U = F(p - 1), V = F(p); n       sum -= got(U) + got(V); n       fa[U] = V, s[V] += s[U], l[V] = l[U]; n       sum += got(V); n       it++; n     } n     ans[qr.second] = sum; n   } n   for (int i = 1; i <= q; i++) printf( %lld n , ans[i]); n } n


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


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 111 * 1000; n int par[MAXN], sz[MAXN]; n int find(int i) { n   if (par[i] == i) return i; n   return par[i] = find(par[i]); n } n void un(int a, int b) { n   sz[find(a)] += sz[find(b)]; n   par[find(b)] = find(a); n } n int n; n int mark[MAXN]; n int sum[MAXN], tot[MAXN]; n vector<int> adj[MAXN]; n int dfs(int pos, int par = -1) { n   if (mark[pos]) return 0; n   mark[pos]++; n   for (__typeof((adj[pos]).begin()) it = (adj[pos]).begin(); n        it != (adj[pos]).end(); ++it) n     if (*it != par) sum[pos] += dfs(*it, pos); n   return sum[pos]; n } n void stot(int pos, int val) { n   if (mark[pos] != 1) return; n   mark[pos]++; n   tot[pos] = val; n   for (__typeof((adj[pos]).begin()) it = (adj[pos]).begin(); n        it != (adj[pos]).end(); ++it) n     stot(*it, val); n } n int main() { n   for (register int i = (0); i < (int)(MAXN); ++i) par[i] = i, sz[i] = 1; n   cin >> n; n   vector<pair<pair<int, int>, pair<int, int> > > E(n - 1); n   for (register int i = (0); i < (int)(n - 1); ++i) n     cin >> E[i].second.first >> E[i].second.second >> E[i].first.first, n         E[i].second.first--, E[i].second.second--, E[i].first.second = i + 1; n   sort((E).begin(), (E).end()); n   int lo = 0; n   long long mb = 0; n   vector<long long> res; n   while (lo < ((int)(E).size())) { n     int hi = lo; n     while (hi < ((int)(E).size()) && E[lo].first.first == E[hi].first.first) n       hi++; n     set<int> st; n     for (register int i = (lo); i < (int)(hi); ++i) { n       int x = find(E[i].second.first), y = find(E[i].second.second); n       if (x != y) { n         st.insert(x); n         st.insert(y); n         adj[x].push_back(y); n         adj[y].push_back(x); n         sum[x] = sz[x]; n         sum[y] = sz[y]; n       } n     } n     for (__typeof((st).begin()) it = (st).begin(); it != (st).end(); ++it) { n       if (!mark[*it]) { n         int tmp = dfs(*it); n         stot(*it, tmp); n       } n     } n     for (__typeof((st).begin()) it = (st).begin(); it != (st).end(); ++it) n       mark[*it] = false, adj[*it].clear(); n     for (register int i = (lo); i < (int)(hi); ++i) { n       int x = E[i].second.first, y = E[i].second.second; n       if (find(x) == find(y)) continue; n       int mn = min(sum[find(x)], sum[find(y)]); n       long long cnt = 2LL * mn * (tot[find(x)] - mn); n       if (cnt > mb) { n         mb = cnt; n         res.clear(); n       } n       if (cnt == mb) res.push_back(E[i].first.second); n     } n     for (register int i = (lo); i < (int)(hi); ++i) { n       int x = E[i].second.first, y = E[i].second.second; n       if (find(x) == find(y)) continue; n       un(x, y); n     } n     lo = hi; n   } n   cout << mb <<     << ((int)(res).size()) << endl; n   for (register int i = (0); i < (int)(((int)(res).size())); ++i) n     cout << res[i] <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void __print(int x) { cout << x; } n void __print(long x) { cout << x; } n void __print(long long x) { cout << x; } n void __print(unsigned x) { cout << x; } n void __print(unsigned long x) { cout << x; } n void __print(unsigned long long x) { cout << x; } n void __print(float x) { cout << x; } n void __print(double x) { cout << x; } n void __print(long double x) { cerr << x; } n void __print(char x) { cout <<      << x <<     ; } n void __print(const char *x) { cout <<     << x <<    ; } n void __print(const string &x) { cout <<     << x <<    ; } n void __print(bool x) { cout << (x ?  true  :  false ); } n template <typename T, typename V> n void __print(const pair<T, V> &x) { n   cout <<  { ; n   __print(x.first); n   cout <<  , ; n   __print(x.second); n   cout <<  } ; n } n template <typename T> n void __print(const T &x) { n   int f = 0; n   cout <<  { ; n   for (auto &i : x) cout << (f++ ?  ,  :   ), __print(i); n   cout <<  } ; n } n void _print() { cout <<  ] n ; } n template <typename T, typename... V> n void _print(T t, V... v) { n   __print(t); n   if (sizeof...(v)) cout <<  ,  ; n   _print(v...); n } n void c_p_c(); n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long int testcase = 1; n   while (testcase--) c_p_c(); n   return 0; n } n void c_p_c() { n   long long int n, m; n   cin >> n >> m; n   vector<vector<long long int>> a(n, vector<long long int>(m, 0)); n   ; n   vector<vector<long long int>> b(n, vector<long long int>(m, 1)); n   ; n   for (long long int i = 0; i < n; ++i) { n     for (long long int j = 0; j < m; ++j) { n       cin >> a[i][j]; n       if (!a[i][j]) { n         for (long long int k = 0; k < n; ++k) { n           b[k][j] = 0; n         } n         for (long long int k = 0; k < m; ++k) { n           b[i][k] = 0; n         } n       } n     } n   } n   for (long long int i = 0; i < n; ++i) { n     for (long long int j = 0; j < m; ++j) { n       if (a[i][j]) { n         long long int ans = 0; n         for (long long int k = 0; k < n; ++k) { n           ans += b[k][j]; n         } n         for (long long int k = 0; k < m; ++k) { n           ans += b[i][k]; n         } n         if ((bool)(ans) == 0) { n           cout <<  NO ; n           return; n         } n       } n     } n   } n   cout <<  YES n ; n   for (long long int i = 0; i < n; ++i) { n     for (long long int j = 0; j < m; ++j) { n       cout << (bool)(b[i][j]) <<    ; n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5001; n const int B = 27; n int zhu[B], n, m; n char s[N], t[N]; n void solve() { n   memset(zhu, 0, sizeof(zhu)); n   n = strlen(s); n   m = strlen(t); n   for (int i = 0; i < n; ++i) zhu[s[i] -  a ]++; n   int r = -1, p; n   if (n > m) n     p = m; n   else n     p = min(n, m) - 1; n   for (int i = t[0] -  a  + 1; i < 26; ++i) n     if (zhu[i]) r = 0; n   for (int i = 1; i <= p; ++i) { n     zhu[t[i - 1] -  a ]--; n     if (zhu[t[i - 1] -  a ] < 0) break; n     if (i == m) { n       r = m; n       continue; n     } n     for (int j = t[i] -  a  + 1; j < 26; ++j) n       if (zhu[j]) r = i; n   } n   if (r == -1) { n     printf( -1 n ); n     return; n   } else { n     memset(zhu, 0, sizeof(zhu)); n     for (int i = 0; i < n; ++i) zhu[s[i] -  a ]++; n     for (int i = 0; i < r; ++i) { n       printf( %c , t[i]); n       zhu[t[i] -  a ]--; n     } n     if (r < m) n       for (int i = t[r] -  a  + 1; i < 26; ++i) n         if (zhu[i]) { n           printf( %c , i +  a ); n           zhu[i]--; n           break; n         } n     for (int i = 0; i < 26; ++i) n       while (zhu[i]--) printf( %c , i +  a ); n     printf(  n ); n   } n } n int main() { n   while (~scanf( %s%s , s, t)) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   char ch = getchar(); n   int x = 0, f = 1; n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while ( 0  <= ch && ch <=  9 ) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n int fac[2000010], inv[2000010], f[2000010]; n inline int Power(int x, int y, int Q) { n   int ret = 1; n   while (y) { n     if (y & 1) ret = 1ll * ret * x % Q; n     x = (1ll * x * x) % Q; n     y >>= 1; n   } n   return ret; n } n int main() { n   int n = read(), m = read(), a = read(), Q = read(); n   int p = 1; n   for (int i = a; i != 1; i = (1ll * i * a % Q), p++) n     ; n   fac[1] = 1; n   for (int i = (2); i <= (max(n, m)); i++) fac[i] = 1ll * fac[i - 1] * i % p; n   inv[0] = inv[1] = 1; n   for (int i = (2); i <= (max(n, m)); i++) n     inv[i] = 1ll * (p - p / i) * inv[p % i] % p; n   for (int i = (2); i <= (max(n, m)); i++) n     inv[i] = 1ll * inv[i - 1] * inv[i] % p; n   f[0] = 1; n   for (int i = (1); i <= (m); i++) { n     f[i] = (f[i - 1] + (1ll * fac[m] * inv[m - i] % p * inv[i] % p)) % p; n   } n   for (int i = (m + 1); i <= (n); i++) f[i] = f[i - 1]; n   for (int i = n; i >= 1; i--) printf( %d  , Power(a, f[i - 1], Q)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, x; n int dist[501][501]; n bool color[501]; n unsigned short num_red; n unsigned short ans[501][501][501], best[501][501], sizes[501]; n unsigned short inf = ~0; n vector<unsigned short> edge[501]; n void dfs(unsigned short& now, unsigned short& before) { n   for (int i = 0; i < edge[now].size(); i++) { n     unsigned short& next = edge[now][i]; n     if (next == before) continue; n     dfs(next, now); n   } n   for (int i = 1; i <= n; i++) n     if (dist[now][i] <= x) { n       sizes[now] = 1; n       ans[now][i][1] = !color[i]; n       for (int j = 0; j < edge[now].size(); j++) { n         unsigned short& next = edge[now][j]; n         if (next == before) continue; n         int num_node; n         if (num_red < sizes[now] + sizes[next]) n           num_node = num_red; n         else n           num_node = sizes[now] + sizes[next]; n         for (unsigned short k = num_node;; k--) { n           unsigned short temp = inf; n           int num_node_1 = k - sizes[now]; n           if (num_node_1 < 0) num_node_1 = 0; n           int num_node_2 = k; n           if (k > sizes[next]) num_node_2 = sizes[next]; n           for (unsigned short j = num_node_1; j <= num_node_2; j++) { n             if (temp > ans[now][i][k - j] + best[next][j]) n               temp = ans[now][i][k - j] + best[next][j]; n           } n           num_node_1++; n           for (unsigned short j = num_node_1; j <= num_node_2; j++) { n             if (temp > ans[now][i][k - j + 1] + ans[next][i][j] - !color[i]) n               temp = ans[now][i][k - j + 1] + ans[next][i][j] - !color[i]; n           } n           ans[now][i][k] = temp; n           if (!k) break; n         } n         sizes[now] += sizes[next]; n       } n     } n   for (unsigned short i = 1; i <= num_red; i++) { n     best[now][i] = inf; n     for (unsigned short j = 1; j <= n; j++) { n       best[now][i] = min(best[now][i], ans[now][j][i]); n     } n   } n } n int main() { n   num_red = 0; n   memset(ans, 0xff, sizeof ans); n   memset(best, 0xff, sizeof best); n   memset(dist, 0x3f3f, sizeof dist); n   cin >> n >> x; n   unsigned short col; n   for (int i = 1; i <= n; i++) { n     cin >> col; n     num_red += col; n     color[i] = col == 1; n   } n   unsigned short a, b; n   int cost; n   for (int i = 1; i < n; i++) { n     cin >> a >> b >> cost; n     edge[a].push_back(b); n     edge[b].push_back(a); n     dist[a][b] = dist[b][a] = cost; n   } n   for (int k = 1; k <= n; k++) { n     for (int i = 1; i <= n; i++) { n       for (int j = 1; j <= n; j++) { n         dist[i][j] = i == j ? 0 : min(dist[i][j], dist[i][k] + dist[k][j]); n       } n     } n   } n   unsigned short start = 1; n   unsigned short before = 0; n   dfs(start, before); n   if (best[1][num_red] == inf) n     cout << -1 << endl; n   else n     cout << best[1][num_red] << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:1024000000,1024000000 ) n using namespace std; n int main() { n   int T, i, j, k, ca = 0, n, m; n   int x1, y1, x2, y2; n   while (~scanf( %d%d%d%d , &x1, &y1, &x2, &y2)) { n     scanf( %d , &n); n     int ans = 0, a, b, c; n     while (n--) { n       scanf( %d%d%d , &a, &b, &c); n       long long first = 1LL * a * x1 + 1LL * b * y1 + c, n                 second = 1LL * a * x2 + 1LL * b * y2 + c; n       if (first > 0 && second < 0) n         ans++; n       else if (first < 0 && second > 0) n         ans++; n     } n     printf( %d n , ans); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e9 + 7; n vector<long long int> v; n long long int calcmulti(string s, long long int l, long long int r) { n   long long int ans = 1; n   string z =   ; n   for (long long int i = l; i <= r; i++) { n     if (s[i] ==  * ) { n       ans *= stoll(z); n       z =   ; n     } else n       z += s[i]; n   } n   ans *= stoll(z); n   return ans; n } n long long int calc(string s, long long int ss, long long int st) { n   if (ss == st) return s[ss] -  0 ; n   long long int mid = (ss + st) / 2, mindist = MAXN; n   for (long long int i = mid; i <= st; i++) { n     if (s[i] ==  + ) { n       mindist = min(mindist, i); n       break; n     } n   } n   for (long long int i = mid; i >= ss; i--) { n     if (s[i] ==  + ) { n       mindist = min(mindist, i); n       break; n     } n   } n   if (mindist == MAXN) return calcmulti(s, ss, st); n   return calc(s, ss, mindist - 1) + calc(s, mindist + 1, st); n } n long long int change(string s, long long int ss, long long int st) { n   string z; n   long long int ans = calc(s, ss, st); n   for (long long int i = 0; i <= ss - 1; i++) z += s[i]; n   z += to_string(ans); n   for (long long int i = st + 1; i <= (long long int)s.length() - 1; i++) n     z += s[i]; n   return calc(z, 0, (long long int)z.length() - 1); n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long int ans = 0; n   string s; n   cin >> s; n   for (long long int i = 0; i < s.length(); i++) n     if (s[i] ==  * ) v.push_back(i); n   if (v.size() == 0) { n     cout << calc(s, 0, (long long int)s.length() - 1); n     return 0; n   } n   if (v.size() == 1) { n     cout << max(change(s, 0, v[0] - 1), n                 max(change(s, v[0] + 1, (long long int)s.length() - 1), n                     calc(s, 0, (long long int)s.length() - 1))); n     ; n     return 0; n   } n   ans = max(ans, calc(s, 0, (long long int)s.length() - 1)); n   for (long long int i = 0; i < (long long int)v.size(); i++) { n     ans = max(ans, change(s, 0, v[i] - 1)); n     ans = max(ans, change(s, v[i] + 1, (long long int)s.length() - 1)); n     for (long long int j = i + 1; j < (long long int)v.size(); j++) { n       ans = max(ans, change(s, v[i] + 1, v[j] - 1)); n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, char *argv[]) { n   unsigned n(0); n   cin >> n; n   vector<unsigned> array(n, 0); n   for (unsigned it(0); it != n; ++it) { n     array[it] = it + 1; n   } n   --n; n   for (unsigned it(n); it > 0; --it) { n     swap(array[it], array[it - 1]); n   } n   ++n; n   for (unsigned it(0); it != n; ++it) { n     cout << array[it] <<    ; n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n long long tab[70]; n void Tab() { n   for (int i = 0; i < 64; i++) tab[i] = pow(2, i); n } n long long Sum(long long n) { n   long long m = n + 1; n   if ((long long)(m) % 2 == 0) n     m /= 2; n   else n     n /= 2; n   long long sum = m * n; n   return sum; n } n long long Sum1(long long n) { n   long long sum1 = pow(2, n) - 1; n   return sum1; n } n int Find(long long n) { n   int i = 0; n   for (; i < 64; i++) { n     if (tab[i] > n) break; n   } n   return i; n } n int main() { n   Tab(); n   long long ans, sum, sum1; n   int t; n   long long n; n   cin >> t; n   while (t--) { n     ans = 0; n     cin >> n; n     sum = Sum(n); n     long long site = Find(n); n     sum1 = Sum1(site); n     ans = sum - sum1 - sum1; n     printf( %lld n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:200000000 ) n using namespace std; n const long long oo = 1LL << 60; n const long long kNumMoves = 4; n const long long kMoves[kNumMoves][2] = {{1, 0}, {-1, 0}, {0, -1}, {0, 1}}; n struct Line { n   Line() : m(0), c(0) {} n   Line(long long slope, long long yOrd) : m(slope), c(yOrd) {} n   long long m; n   long long c; n }; n class ConvexHullOptimization { n  public: n   ConvexHullOptimization(int n) : cur(0), lines(n), count(0) {} n   void Add(long long m, long long c) { n     Line newLine(m, c); n     while (!KeepCurrentTop(newLine)) --count; n     lines[count++] = newLine; n   } n   void Reset() { cur = count = 0; } n   long long GetValue(long long x) { n     if (cur >= count) cur = count - 1; n     while ((cur < count - 1) && ((lines[cur].m * x + lines[cur].c) >= n                                  (lines[cur + 1].m * x + lines[cur + 1].c))) n       ++cur; n     return lines[cur].m * x + lines[cur].c; n   } n  n  private: n   bool KeepCurrentTop(const Line& l3) { n     if ((count - cur) < 2) return true; n     Line l2 = lines[count - 1]; n     Line l1 = lines[count - 2]; n     long double newCost = (l3.c - l1.c) * (long double)(l1.m - l2.m); n     long double oldCost = (l2.c - l1.c) * (long double)(l1.m - l3.m); n     return newCost > oldCost; n   } n   int cur; n   int count; n   vector<Line> lines; n }; n int n, m, p; n vector<long long> d, t, accum; n long long GetMinWaitingTime() { n   vector<vector<long long> > dp(m, vector<long long>(2, 0)); n   for (int i = 0; i < int(m); ++i) dp[i][0] = (i + 1) * t[i] - accum[i]; n   long long res = oo; n   ConvexHullOptimization chOpt(m + 1); n   for (int i = 1; i < int(p); ++i) { n     int curIdx = (i & 1); n     int prevIdx = curIdx ^ 1; n     chOpt.Reset(); n     chOpt.Add(-1, accum[0] + dp[0][prevIdx]); n     for (int k = 1; k < int(m); ++k) { n       dp[k][curIdx] = dp[k][prevIdx]; n       dp[k][curIdx] = n           min(dp[k][curIdx], (k + 1) * t[k] - accum[k] + chOpt.GetValue(t[k])); n       chOpt.Add(-(k + 1), accum[k] + dp[k][prevIdx]); n     } n     res = min(res, dp[m - 1][curIdx]); n   } n   return res; n } n int main() { n   cin >> n >> m >> p; n   d.resize(n, 0); n   for (int i = 0; i < int(n - 1); ++i) cin >> d[i + 1]; n   accum.resize(n, 0); n   for (int i = 0; i < int(n - 1); ++i) accum[i + 1] = accum[i] + d[i + 1]; n   t.resize(m); n   for (int i = 0; i < int(m); ++i) { n     int h; n     cin >> h >> t[i]; n     t[i] -= accum[--h]; n   } n   sort(t.begin(), t.end()); n   long long mn = t.front(); n   for (int i = 0; i < int(m); ++i) t[i] -= mn; n   accum.clear(); n   accum.resize(m, 0); n   accum[0] = t[0]; n   for (int i = 0; i < int(m - 1); ++i) accum[i + 1] = accum[i] + t[i + 1]; n   long long res = GetMinWaitingTime(); n   cout << res; n   int i; n   cin >> i; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, in[5100], st[5100], f[5100], ans = 9999999999999; n signed main() { n   cin >> n; n   for (long long i = 1; i <= n; i++) { n     cin >> in[i]; n   } n   memset(f, 0x7f, sizeof(f)); n   memcpy(st, in, sizeof(in)); n   sort(st + 1, st + n + 1); n   for (long long i = 1; i <= n; i++) f[i] = 0; n   for (long long i = 1; i <= n; i++) { n     for (long long o = 1; o <= n; o++) { n       f[o] = min(f[o - 1], f[o] + abs(in[i] - st[o])); n     } n   } n   for (long long i = 1; i <= n; i++) { n     ans = min(ans, f[i]); n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n double f[4][207][207]; n struct node { n   int x, y; n }; n node a[207]; n bool cmp(const node& x, const node& y) { return x.y > y.y; } n int n, m, bm; n int main() { n   while (scanf( %d %d %d , &n, &m, &bm) != EOF) { n     int i, j, k, r, w; n     int sum = 0; n     memset(f, 0, sizeof(f)); n     sum = bm; n     for (i = 1; i <= n; i++) { n       scanf( %d  , &a[i].x); n     } n     for (i = 1; i <= n; i++) scanf( %d , &a[i].y); n     sort(a + 1, a + n + 1, cmp); n     double ans = 0; n     memset(f, 0, sizeof(f)); n     f[0][0][bm] = 1; n     for (i = 0; i < n; i++) { n       w = (i + 1) & 1; n       r = (i)&1; n       memset(f[w], 0, sizeof(f[w])); n       for (j = 0; j <= i; j++) n         for (k = 0; k <= 200; k++) { n           int h = k + a[i + 1].y; n           if (h < 0) continue; n           if (h > 200) h = 200; n           f[w][j + 1][h] += f[r][j][k] * (double)a[i + 1].x / 100; n         } n       for (j = 0; j <= i; j++) n         for (k = 0; k <= 200; k++) { n           int h = k + a[i].y; n           f[w][j][k] += f[r][j][k] * (1 - (double)a[i + 1].x / 100); n         } n     } n     w = n & 1; n     for (j = m; j <= n; j++) n       for (k = 0; k <= 200; k++) ans += f[w][j][k]; n     printf( %f n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int x, cnt; n int main() { n   cin >> x; n   while (x > 0) { n     if (x % 2 == 1) { n       cnt++; n     } n     x /= 2; n   } n   cout << cnt << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n; n   cin >> n; n   long long int num1 = n / 3; n   long long int num2 = n - (2 * num1); n   if (num1 % 3 != 0 && num2 % 3 != 0) n     cout << num1 <<     << num1 <<     << num2; n   else if (num1 - 1 > 0 && (num1 - 1) % 3 != 0 && (num2 + 2) % 3 != 0) n     cout << num1 - 1 <<     << num1 - 1 <<     << num2 + 2; n   else if (num2 - 2 > 0 && (num1 + 1) % 3 != 0 && (num2 - 2) % 3 != 0) n     cout << num1 + 1 <<     << num1 + 1 <<     << num2 - 2; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, c[5555], num[5555], ans[5555]; n int main() { n   while (~scanf( %d , &n)) { n     for (int i = 1; i <= n; i++) scanf( %d , &c[i]); n     memset(num, 0, sizeof(num)); n     memset(ans, 0, sizeof(ans)); n     for (int i = 1; i <= n; i++) { n       memset(num, 0, sizeof(num)); n       num[c[i]]++, ans[c[i]]++; n       int Max = c[i]; n       for (int j = i + 1; j <= n; j++) { n         num[c[j]]++; n         if (num[c[j]] > num[Max] || num[c[j]] == num[Max] && c[j] < Max) n           Max = c[j]; n         ans[Max]++; n       } n     } n     for (int i = 1; i <= n; i++) printf( %d%c , ans[i], i == n ?   n  :    ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n T in() { n   char ch; n   T n = 0; n   bool ng = false; n   while (1) { n     ch = getchar(); n     if (ch ==  - ) { n       ng = true; n       ch = getchar(); n       break; n     } n     if (ch >=  0  && ch <=  9 ) break; n   } n   while (1) { n     n = n * 10 + (ch -  0 ); n     ch = getchar(); n     if (ch <  0  || ch >  9 ) break; n   } n   return (ng ? -n : n); n } n template <typename T> n inline T POW(T B, T P) { n   if (P == 0) return 1; n   if (P & 1) n     return B * POW(B, P - 1); n   else n     return (POW(B, P / 2) * POW(B, P / 2)); n } n template <typename T> n inline T Gcd(T a, T b) { n   if (a < 0) return Gcd(-a, b); n   if (b < 0) return Gcd(a, -b); n   return (b == 0) ? a : Gcd(b, a % b); n } n template <typename T> n inline T Lcm(T a, T b) { n   if (a < 0) return Lcm(-a, b); n   if (b < 0) return Lcm(a, -b); n   return a * (b / Gcd(a, b)); n } n long long Bigmod(long long base, long long power, long long MOD) { n   long long ret = 1; n   while (power) { n     if (power & 1) ret = (ret * base) % MOD; n     base = (base * base) % MOD; n     power >>= 1; n   } n   return ret; n } n bool isVowel(char ch) { n   ch = toupper(ch); n   if (ch ==  A  || ch ==  U  || ch ==  I  || ch ==  O  || ch ==  E ) n     return true; n   return false; n } n template <typename T> n long long int isLeft(T a, T b, T c) { n   return (a.x - b.x) * (b.y - c.y) - (b.x - c.x) * (a.y - b.y); n } n long long ModInverse(long long number, long long MOD) { n   return Bigmod(number, MOD - 2, MOD); n } n bool isConst(char ch) { n   if (isalpha(ch) && !isVowel(ch)) return true; n   return false; n } n int toInt(string s) { n   int sm; n   stringstream second(s); n   second >> sm; n   return sm; n } n int a[3][77]; n int Ln[3]; n int n; n int dp[77][77][77][2]; n int lol(int v, int k, int x, int f) { n   if ((v + k + x) == n) return 0; n   int &res = dp[v][k][x][f]; n   if (res != -1) return res; n   res = 20000; n   int np = v + k + x + 1; n   if (v < Ln[1]) { n     int tp = 0; n     int ps = a[1][v]; n     for (int i = x; i < Ln[0]; i++) { n       if (a[0][i] < ps) n         tp++; n       else n         break; n     } n     for (int i = k; i < Ln[2]; i++) { n       if (a[2][i] < ps) n         tp++; n       else n         break; n     } n     res = min(res, tp + lol(v + 1, k, x, 1)); n   } n   if (!f) { n     if (k < Ln[2]) { n       int tp = 0; n       int ps = a[2][k]; n       for (int i = x; i < Ln[0]; i++) { n         if (a[0][i] < ps) n           tp++; n         else n           break; n       } n       for (int i = v; i < Ln[1]; i++) { n         if (a[1][i] < ps) n           tp++; n         else n           break; n       } n       res = min(res, tp + lol(v, k + 1, x, 0)); n     } n   } n   if (x < Ln[0]) { n     int tp = 0; n     int ps = a[0][x]; n     for (int i = v; i < Ln[1]; i++) { n       if (a[1][i] < ps) n         tp++; n       else n         break; n     } n     for (int i = k; i < Ln[2]; i++) { n       if (a[2][i] < ps) n         tp++; n       else n         break; n     } n     res = min(res, tp + lol(v, k, x + 1, 0)); n   } n   return res; n } n map<char, int> mp; n int main() { n   mp[ V ] = 1; n   mp[ K ] = 2; n   string s; n   cin >> n; n   cin >> s; n   for (int i = 1; i <= n; i++) { n     int tp = mp[s[i - 1]]; n     a[tp][Ln[tp]] = i; n     Ln[tp]++; n   } n   memset(dp, -1, sizeof(dp)); n   cout << lol(0, 0, 0, 0) << endl; n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n  n #define all(x)       (x).begin(), (x).end() n #define acarr(arr, n, type)     type *arr=new type[n]; for(long long i=0; i<n; ++i) cin >> arr[i]; n #define ll       long long n #define TF(ok)      cout << (ok ?  YES n  :  NO n ) n #define ld       long double n #define vi       vector<int> v1 n #define pb       push_back n  n int solve(){ n   n  ll ans=0, tab=0; n  n  vector<int> v1(3); n  for (int i=0; i<3; i++){ n   cin >> v1[i]; n   ans+=v1[i]; n  } n  n  if (ans%9==0){ n   tab= ans/9; n   for (int i=0; i<3; i++){ n    if (v1[i]<tab){ n     cout <<  NO n  ; n     return 0; n    } n   } n   cout <<  YES n ; n  } n  else{ n   cout <<  NO n  ; n  } n  n  n  return 0; n } n  n  n int main(){ n  ios::sync_with_stdio(false); n  cin.tie(0); n  int q;  n  q=1; n cin >> q; n  while(q--){ n   solve(); n  } n  n  return 0; n } n  n /*   ~ Check for overflow n  ~ Division by 0 n  ~ Uninitialized n  ~ n */ n


#include <bits/stdc++.h> n using namespace std; n const long long INF = numeric_limits<long long>::max() / 4; n struct MinCostMaxFlow { n   int N; n   vector<vector<long long> > cap, flow, cost; n   vector<int> found; n   vector<long long> dist, pi, width; n   vector<pair<int, int> > dad; n   MinCostMaxFlow(int N) n       : N(N), n         cap(N, vector<long long>(N)), n         flow(N, vector<long long>(N)), n         cost(N, vector<long long>(N)), n         found(N), n         dist(N), n         pi(N), n         width(N), n         dad(N) {} n   void AddEdge(int from, int to, long long cap, long long cost) { n     this->cap[from][to] = cap; n     this->cost[from][to] = cost; n   } n   void Relax(int s, int k, long long cap, long long cost, int dir) { n     long long val = dist[s] + pi[s] - pi[k] + cost; n     if (cap && val < dist[k]) { n       dist[k] = val; n       dad[k] = make_pair(s, dir); n       width[k] = min(cap, width[s]); n     } n   } n   long long Dijkstra(int s, int t) { n     fill(found.begin(), found.end(), false); n     fill(dist.begin(), dist.end(), INF); n     fill(width.begin(), width.end(), 0); n     dist[s] = 0; n     width[s] = INF; n     while (s != -1) { n       int best = -1; n       found[s] = true; n       for (int k = 0; k < N; k++) { n         if (found[k]) continue; n         Relax(s, k, cap[s][k] - flow[s][k], cost[s][k], 1); n         Relax(s, k, flow[k][s], -cost[k][s], -1); n         if (best == -1 || dist[k] < dist[best]) best = k; n       } n       s = best; n     } n     for (int k = 0; k < N; k++) pi[k] = min(pi[k] + dist[k], INF); n     return width[t]; n   } n   pair<long long, long long> GetMaxFlow(int s, int t) { n     long long totflow = 0, totcost = 0; n     while (long long amt = Dijkstra(s, t)) { n       totflow += amt; n       for (int x = t; x != s; x = dad[x].first) { n         if (dad[x].second == 1) { n           flow[dad[x].first][x] += amt; n           totcost += amt * cost[dad[x].first][x]; n         } else { n           flow[x][dad[x].first] -= amt; n           totcost -= amt * cost[x][dad[x].first]; n         } n       } n     } n     return make_pair(totflow, totcost); n   } n }; n const int N = 105; n char s[N]; n int b[N]; n int main() { n   int n; n   cin >> n; n   scanf( %s , s + 1); n   int sum = 0; n   int cnt[26]; n   for (int i = 0; i < 26; i++) cnt[i] = 0; n   for (int i = 1; i <= n; i++) { n     scanf( %d , b + i); n     sum += b[i]; n     cnt[s[i] -  a ]++; n   } n   MinCostMaxFlow mcmf(1 + 26 + n / 2 + 1 + 1); n   for (int i = 1; i <= 26; i++) { n     mcmf.AddEdge(1, i + 1, cnt[i - 1], 0); n   } n   for (int i = 1; i <= n / 2; i++) { n     mcmf.AddEdge(27 + i, 1 + 26 + n / 2 + 1, 2, 0); n   } n   for (int i = 1; i <= n / 2; i++) { n     for (int j = 0; j < 26; j++) { n       char k =  a  + j; n       if (s[i] == k && s[n - i + 1] == k) { n         mcmf.AddEdge(2 + j, 27 + i, 1, sum - max(b[i], b[n - i + 1])); n       } else if (s[i] == k) { n         mcmf.AddEdge(2 + j, 27 + i, 1, sum - b[i]); n       } else if (s[n - i + 1] == k) { n         mcmf.AddEdge(2 + j, 27 + i, 1, sum - b[n - i + 1]); n       } else { n         mcmf.AddEdge(2 + j, 27 + i, 1, sum); n       } n     } n   } n   auto e = mcmf.GetMaxFlow(1, 1 + 26 + n / 2 + 1); n   cout << n * sum - e.second << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, c; n   scanf( %d %d %d , &a, &b, &c); n   bool valid = false; n   int bo1, bo2, bo3; n   for (int i = 0; i <= a; i++) { n     bo1 = i; n     bo3 = a - i; n     bo2 = c - bo3; n     if (bo1 + bo3 != a || bo1 + bo2 != b || bo2 + bo3 != c) continue; n     if (bo1 < 0 || bo2 < 0 || bo3 < 0) continue; n     valid = true; n     break; n   } n   if (!valid) n     puts( Impossible ); n   else n     printf( %d %d %d n , bo1, bo2, bo3); n   return 0; n } n


#include <bits/stdc++.h> n const int mod = 998244353; n const int N = 1e6 + 7; n using namespace std; n long long n, m, k, i, j, t, sig = 0, ans; n long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } n map<pair<int, int>, set<long long>> mp; n long long x[1005], y[1005]; n struct node { n   int x, pos; n } p[500007]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   long long ans = 0, sum = 0; n   for (i = 1; i <= n; i++) cin >> x[i] >> y[i]; n   for (i = 1; i <= n - 1; i++) { n     for (j = i + 1; j <= n; j++) { n       long long x1 = x[i], x2 = x[j], y1 = y[i], y2 = y[j]; n       int b = x1 - x2, a = y1 - y2; n       long long d = gcd(a, b); n       a /= d, b /= d; n       if (a < 0 || (!a && b < 0)) { n         a = -a, b = -b; n       } n       pair<int, int> line(a, b); n       long long c = a * x1 - b * y1; n       if (!mp[line].count(c)) { n         sum++; n         mp[line].insert(c); n         ans += sum - mp[line].size(); n       } n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int q; n   cin >> q; n   for (int i = 0; i < q; i++) { n     int a, b, c; n     cin >> a; n     cin >> b; n     cin >> c; n     int p1, p2; n     cin >> p1; n     cin >> p2; n     int min_no = min(b, c); n     if (a <= 1) { n       cout << 0 <<   n ; n       continue; n     } n     if (b * 2 + c * 2 > a) { n       int p = 0; n       if (p2 >= p1) { n         if (c * 2 > a) { n           cout << (a / 2) * p2 <<   n ; n           continue; n         } else { n           p += c * p2; n           if ((a - c * 2) <= 1) { n             cout << p <<   n ; n             continue; n           } n           p += ((a - c * 2) / 2) * p1; n           cout << p <<   n ; n           continue; n         } n       } else if (p1 > p2) { n         if (b * 2 > a) { n           cout << (a / 2) * p1 <<   n ; n           continue; n         } else { n           p += b * p1; n           if ((a - b * 2) <= 1) { n             cout << p <<   n ; n             continue; n           } n           p += ((a - b * 2) / 2) * p2; n           cout << p <<   n ; n           continue; n         } n       } n     } else { n       int p = 0; n       p += (p2 * c) + (p1 * b); n       cout << p <<   n ; n       continue; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, h, t, u, v, tim, d[100100], w[100100], W[100100]; n vector<int> e[100100], ans; n int main() { n   scanf( %d%d%d%d , &n, &m, &h, &t); n   for (int i = 0; i < m; i++) n     scanf( %d%d , &u, &v), e[u].push_back(v), e[v].push_back(u); n   for (int i = 1; i < n + 1; i++) d[i] = e[i].size(); n   for (int i = 1; i < n + 1; i++) { n     if (d[i] <= h) continue; n     for (int j : e[i]) w[j] = i; n     for (int j : e[i]) { n       tim++; n       int p = d[i] - 1 - h, q = 0; n       ans.clear(); n       for (int k : e[j]) { n         if (k != i) n           if (p && w[k] == i) n             p--, q++, ans.push_back(k), W[k] = tim; n           else if (w[k] != i) n             q++, ans.push_back(k), W[k] = tim; n         if (q == t) break; n       } n       if (q == t) { n         puts( YES ); n         printf( %d %d n , i, j); n         int p = 0; n         for (int k : e[i]) n           if (k != j && W[k] != tim) { n             p++, printf( %d  , k); n             if (p == h) break; n           } n         puts(  ); n         for (int k : ans) printf( %d  , k); n         return 0; n       } n     } n   } n   puts( NO ); n } n


#include <bits/stdc++.h> n int main() { n   int x, y; n   scanf( %d%d , &x, &y); n   double r = sqrt(x * x + y * y); n   int rmin = r; n   if ((double)rmin == r) { n     printf( black ); n     return 0; n   } n   if (x == 0 || y == 0) { n     printf( black ); n     return 0; n   } n   double tan = (double)y / (double)x; n   if (rmin % 2 == 1 && tan > 0) n     printf( white ); n   else if (rmin % 2 == 1 && tan < 0) n     printf( black ); n   else if (rmin % 2 == 0 && tan > 0) n     printf( black ); n   else if (rmin % 2 == 0 && tan < 0) n     printf( white ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3e5 + 5; n int n, m, a, b, g; n int dis[N]; n long long ans, c[N]; n long long sum(int u) { return 1LL * u * (u + 1) / 2; } n long long query(int u) { n   return sum(u / g + 1) * g - 1LL * (u / g + 1) * ((g - ((u + 1) % g)) % g); n } n int gcd(int u, int v) { n   if (v == 0) { n     return u; n   } n   return gcd(v, u % v); n } n set<pair<int, int> > s; n int main() { n   scanf( %d%d%d , &n, &a, &b); n   g = gcd(a, b); n   if (n > a + b - g) { n     ans += query(n) - query(a + b - g); n     n = a + b - g; n   } n   memset(dis, 0x3f, sizeof dis); n   dis[0] = 0; n   s.insert({0, 0}); n   while (!s.empty()) { n     auto u = *s.begin(); n     s.erase(s.begin()); n     if (dis[u.second] != u.first) { n       continue; n     } n     ++c[u.first]; n     if (u.second >= b && u.first < dis[u.second - b]) { n       dis[u.second - b] = u.first; n       s.insert({u.first, u.second - b}); n     } n     if (u.second + a <= n && max(u.first, u.second + a) < dis[u.second + a]) { n       dis[u.second + a] = max(u.first, u.second + a); n       s.insert({max(u.first, u.second + a), u.second + a}); n     } n   } n   for (int i = 1; i <= n; i++) { n     c[i] += c[i - 1]; n   } n   for (int i = 0; i <= n; i++) { n     ans += c[i]; n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, first, a[1111]; n int b[1111], ub = -1, pos; n vector<int> c[1111]; n vector<int> d; n int can[1111]; n void go(int p, int v) { n   if (v == -1) return; n   if (v == first) ub = p; n   c[p].push_back(v); n   go(p, a[v]); n } n int main() { n   scanf( %d%d , &n, &first); n   --first; n   for (int i = 0; i < n; ++i) b[i] = -1; n   for (int i = 0; i < n; ++i) { n     scanf( %d , a + i); n     a[i]--; n     b[a[i]] = i; n   } n   for (int i = 0; i < n; ++i) { n     c[i].clear(); n     if (b[i] == -1) { n       go(i, i); n     } n   } n   assert(ub >= 0); n   d.clear(); n   for (int i = 0; i < c[ub].size(); ++i) { n     if (c[ub][i] == first) pos = c[ub].size() - i; n   } n   for (int i = 0; i < n; ++i) { n     if (i == ub) continue; n     if (c[i].size() != 0) d.push_back(c[i].size()); n   } n   sort(d.begin(), d.end()); n   for (int i = 0; i < n; ++i) can[i] = 0; n   can[0] = 1; n   for (int i = 0; i < d.size(); ++i) { n     for (int j = n - 1; j >= 0; --j) { n       if ((j + d[i] < n) && (can[j] == 1)) can[j + d[i]] = 1; n     } n   } n   for (int i = 0; i < n; ++i) { n     if (can[i]) cout << i + pos << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, i, maxi = 0; n   cin >> n; n   if (n == 1) { n     cout <<  1  << endl; n     return 0; n   } n   vector<int> a(n, 0); n   vector<int> right(n, 0); n   vector<int> left(n, 0); n   vector<int> ans(n, 0); n   for (i = 0; i < n; i++) scanf(  %d , &a[i]); n   for (i = 1; i < n; i++) n     if (a[i] > a[i - 1]) left[i] += left[i - 1] + 1; n   for (i = n - 2; i >= 0; i--) n     if (a[i] < a[i + 1]) right[i] += right[i + 1] + 1; n   for (i = 1; i < n - 1; i++) { n     if (a[i - 1] < a[i] && a[i] < a[i + 1]) n       ans[i] = left[i] + right[i] + 1; n     else if (a[i + 1] > a[i - 1] + 1) n       ans[i] = left[i - 1] + right[i + 1] + 3; n     else n       ans[i] = max(left[i - 1], right[i + 1]) + 2; n   } n   ans[0] = (a[0] < a[1]) ? right[0] + 1 : right[1] + 2; n   ans[n - 1] = (a[n - 1] > a[n - 2]) ? left[n - 1] + 1 : left[n - 2] + 2; n   for (i = 0; i < n; i++) n     if (ans[i] > maxi) maxi = ans[i]; n   cout << maxi << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n, sum, as, b[100005], len, ia, la; n pair<int, int> a[100005]; n int main() { n   cin >> n; n   for (int i = 0; i < n; ++i) { n     cin >> a[i].first; n     a[i].second = i + 1; n     sum += a[i].first; n   } n   sort(a, a + n); n   la = n / 2; n   for (int i = 0; i < n / 2; ++i) as += a[i].first; n   for (int d = (sum - a[n - 1].first + 1) / 2; as < d; n        as -= a[ia++].first, as += a[la++].first) n     ; n   cout << la - ia << endl; n   for (int i = 0; i < n; ++i) { n     if (i < ia || i >= la) n       b[len++] = a[i].second; n     else { n       if (i - ia) cout <<    ; n       cout << a[i].second; n     } n   } n   cout << endl; n   cout << len << endl; n   for (int i = 0; i < len; ++i) { n     if (i) cout <<    ; n     cout << b[i]; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n const double PI = acos(-1); n int N; n long long A; n struct point { n   long long x, y; n } p[100001]; n struct line { n   double start, end; n } q[100001]; n inline long long sqr(long long x) { return x * x; } n inline double tan_360(double x, double y) { n   if (x == 0) return PI * (y > 0 ? 0.5 : 1.5); n   if (y == 0) return PI * (x > 0 ? 0.0 : 1.0); n   double ans = atan(y / x); n   if (x > 0 && y > 0) return ans; n   if (x < 0 && y > 0) return ans + PI; n   if (x < 0 && y < 0) return ans + PI; n   return ans + PI * 2; n } n bool check(double mid) { n   int L = 0; n   for (int i = 1; i <= N; i++) n     if (sqrt(sqr(A + p[i].x) + sqr(p[i].y)) <= n         mid - sqrt(sqr(A - p[i].x) + sqr(p[i].y))) n       return true; n   for (int i = 1; i <= N; i++) { n     double R1 = mid, R2 = sqrt(sqr(A - p[i].x) + sqr(p[i].y)), n            dis = sqrt(sqr(A + p[i].x) + sqr(p[i].y)); n     if (R1 + R2 > dis && R1 + dis > R2 && R2 + dis > R1) { n       double mid_angle = tan_360(p[i].x + A, p[i].y), n              dif = acos((R1 * R1 + dis * dis - R2 * R2) / (2 * R1 * dis)); n       q[++L] = {mid_angle < dif ? mid_angle - dif + 2 * PI : mid_angle - dif, n                 mid_angle + dif >= 2 * PI ? mid_angle + dif - 2 * PI n                                           : mid_angle + dif}; n       if (q[L].start > q[L].end) std::swap(q[L].start, q[L].end); n     } n   } n   std::sort(q + 1, q + L + 1, [](const line &a, const line &b) { n     return a.start < b.start || a.start == b.start && a.end < b.end; n   }); n   static int stack[100001]; n   int D = 0; n   for (int i = 1; i <= L; i++) { n     while (D && q[stack[D]].end < q[i].start) D--; n     if (D && q[stack[D]].end < q[i].end) return true; n     stack[++D] = i; n   } n   return false; n } n int main() { n   scanf( %d%lld , &N, &A); n   for (int i = 1; i <= N; i++) scanf( %lld%lld , &p[i].x, &p[i].y); n   double l = 0, r = A * 2; n   for (int T = 55; T--;) { n     double mid = (l + r) / 2; n     (check(mid) ? r : l) = mid; n   } n   printf( %.10lf n , l); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 1000100; n struct node { n   int w, flag; n   int a, b; n   int id; n   long long num; n } a[M]; n int n, pos[M], m, s[M]; n bool cmp(const node &x, const node &y) { n   if (x.w == y.w) return x.flag > y.flag; n   return x.w < y.w; n } n int fa[M]; n bool cmp1(const node &x, const node &y) { return x.id < y.id; } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; i++) { n     scanf( %d%d , &a[i].w, &a[i].flag); n     a[i].id = i; n   } n   sort(a + 1, a + 1 + m, cmp); n   for (int i = 1; i <= n; i++) fa[i] = i; n   int k = 1; n   for (int i = 1; i <= m; i++) n     if (a[i].flag) { n       a[i].a = k; n       pos[k] = i; n       a[i].num = (long long)(k - 1) * k / 2 + 1; n       a[i].b = ++k; n     } n   bool flag = 0; n   for (int i = 1; i <= m; i++) n     if (a[i].flag) n       if (a[i].num < (long long)i) { n         flag = 1; n         break; n       } n   if (flag) { n     printf( -1 n ); n     return 0; n   } n   pos[n] = m + 1; n   int h = 3, s1 = 1; n   for (int i = 1; i < n; i++) { n     for (int j = pos[i] + 1; j < pos[i + 1];) { n       a[j].a = s1; n       a[j].b = h; n       s1++; n       if (s1 == h - 1) { n         s1 = 1; n         h++; n       } n       j++; n     } n   } n   if (flag) { n     printf( -1 n ); n     return 0; n   } n   sort(a + 1, a + 1 + m, cmp1); n   for (int i = 1; i <= m; i++) printf( %d %d n , a[i].a, a[i].b); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 605; n int nask(vector<int> tmp) { n   if (!tmp.size() || tmp.size() == 1) return 0; n   cout <<  ?   << tmp.size() << endl; n   for (int i = (0), iend = ((int)tmp.size() - 1); i <= iend; i++) n     cout << tmp[i] <<    ; n   cout << endl; n   cout.flush(); n   int ans; n   cin >> ans; n   if (ans == -1) exit(0); n   return ans; n } n int dask(int u, vector<int> tar) { n   int cntsig = nask(tar); n   tar.push_back(u); n   int cnttot = nask(tar); n   return cnttot - cntsig; n } n int n; n bool vis[maxn]; n int bl[maxn]; n int find(int u, vector<int> tmp) { n   vector<int> c1, c2; n   if (tmp.size() == 1) return tmp[0]; n   for (int i = (0), iend = ((int)(tmp.size()) - 1); i <= iend; i++) { n     if (c1.size() < tmp.size() / 2) n       c1.push_back(tmp[i]); n     else n       c2.push_back(tmp[i]); n   } n   if (dask(u, c1)) n     return find(u, c1); n   else n     return find(u, c2); n } n vector<int> half[2]; n vector<int> side[maxn]; n void dfs(int u) { n   half[bl[u]].push_back(u); n   while (1) { n     vector<int> tmp; n     for (int i = (1), iend = (n); i <= iend; i++) n       if (!vis[i]) tmp.push_back(i); n     if (dask(u, tmp) == 0) return; n     int tar = find(u, tmp); n     side[u].push_back(tar); n     side[tar].push_back(u); n     vis[tar] = 1; n     bl[tar] = bl[u] ^ 1; n     dfs(tar); n   } n } n int stk[maxn], top; n vector<int> ans; n void cfs(int u, int tar) { n   stk[++top] = u; n   if (u == tar) { n     for (int i = (1), iend = (top); i <= iend; i++) ans.push_back(stk[i]); n     return; n   } n   for (int i = (0), iend = (side[u].size() - 1); i <= iend; i++) { n     int v = side[u][i]; n     if (!vis[v]) { n       vis[v] = 1; n       cfs(v, tar); n     } n   } n   top--; n } n void ans_out(int u, int v) { n   memset(vis, 0, sizeof(vis)); n   vis[u] = 1; n   cfs(u, v); n   cout <<  N   << ans.size() << endl; n   for (int i = (0), iend = ((int)(ans.size()) - 1); i <= iend; i++) n     cout << ans[i] <<    ; n   return; n } n int main() { n   ios::sync_with_stdio(0); n   cin >> n; n   vis[1] = 1; n   dfs(1); n   if (!nask(half[0]) && !nask(half[1])) { n     cout <<  Y   << half[0].size() << endl; n     for (int i = (0), iend = ((int)(half[0].size()) - 1); i <= iend; i++) n       cout << half[0][i] <<    ; n     return 0; n   } else { n     for (int i = (0), iend = (1); i <= iend; i++) n       for (int j = (0), jend = ((int)(half[i].size()) - 1); j <= jend; j++) { n         vector<int> tmp; n         int u = half[i][j]; n         for (int k = (0), kend = ((int)(half[i].size()) - 1); k <= kend; k++) n           if (half[i][k] != u) tmp.push_back(half[i][k]); n         if (dask(u, tmp)) { n           for (int k = (0), kend = ((int)(tmp.size()) - 1); k <= kend; k++) { n             vector<int> gg; n             gg.push_back(u); n             gg.push_back(tmp[k]); n             if (nask(gg)) { n               ans_out(u, tmp[k]); n               return 0; n             } n           } n         } n       } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int a[200010]; n int prv[200010]; n int f[200010][10]; n void FillAll(int l, int r, int x, int y) { n   for (int i = x, j = l; i <= y; i++) { n     int ci = (r - j + 1) / (y - i + 1); n     ci = max(ci, 2); n     ci = min(ci, 5); n     for (int jj = j; jj < j + ci; jj++) a[jj] = i; n     j += ci; n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , a + i); n   } n   for (int i = 1; i <= n; i++) prv[i] = a[i - 1] ? i - 1 : prv[i - 1]; n   memset(f, -1, sizeof f); n   f[0][5] = 0; n   int last = 0; n   for (int i = 1; i <= n; i++) n     if (a[i]) { n       last = i; n       int j = prv[i]; n       int space = i - j - 1; n       if (a[i] > a[j]) { n         for (int ki = 0; ki < min(5, space + 1); ki++) n           for (int kj1 = 1; kj1 <= 5 && f[i][ki + 1] == -1; kj1++) n             if (f[j][kj1] >= 0) { n               for (int kj2 = max(0, 2 - kj1); kj2 <= min(space - ki, 5 - kj1); n                    kj2++) { n                 int least = kj2 + 2 * (a[i] - a[j] - 1) + ki; n                 int most = kj2 + 5 * (a[i] - a[j] - 1) + ki; n                 if (least <= space && space <= most) { n                   f[i][ki + 1] = kj2; n                   break; n                 } n               } n             } n       } n       if (a[i] == a[j]) { n         int ki = space + 1; n         for (int kj = 1; kj <= 5 - ki; kj++) n           if (f[j][kj] >= 0) f[i][ki + kj] = 0; n       } n       if (a[i] < a[j]) { n         printf( -1 ); n         return 0; n       } n     } n   if (last) { n     int had = last == n - 1; n     for (int i = last; i; i = prv[i]) { n       int j = prv[i]; n       if (a[i] != a[j]) { n         int lbound = max(1, 2 - had); n         int rbound = 5 - had; n         if (i == last) { n           lbound = 1 + (last == n); n           rbound = 5 - (last == n - 1); n         } n         bool isRet = 0; n         for (int ki = rbound; ki >= lbound; ki--) n           if (f[i][ki] >= 0) { n             for (int k = i - ki + 1; k <= i; k++) a[k] = a[i]; n             for (int k = j + 1; k <= j + f[i][ki]; k++) a[k] = a[j]; n             FillAll(j + f[i][ki] + 1, i - ki, a[j] + 1, a[i] - 1); n             had = f[i][ki]; n             isRet = 1; n             break; n           } n         if (!isRet) { n           printf( -1 ); n           return 0; n         } n       } else { n         for (int k = j + 1; k < i; k++) a[k] = a[i]; n         had += i - j; n       } n     } n     if (a[last] != a[last - 1]) { n       a[last + 1] = a[last]; n       last++; n     } n     if (last > n) { n       printf( -1 ); n       return 0; n     } n   } n   for (int i = last + 1; i <= n; i++) n     if (i == 1 || a[i - 1] == a[i - 2]) n       a[i] = a[i - 1] + 1; n     else n       a[i] = a[i - 1]; n   if (a[n] != a[n - 1]) a[n] = a[n - 1]; n   printf( %d n , a[n]); n   for (int i = 1; i <= n; i++) printf( %d  , a[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 998244353; n const int N = 1e6 + 5; n long long p[N]; n long long cnt[N]; n vector<long long> a[N]; n long long qmi(long long a, long long b) { n   long long ans = 1; n   while (b) { n     if (b & 1) ans = ans * a % mod; n     b >>= 1; n     a = a * a % mod; n   } n   return ans % mod; n } n int main() { n   long long n, k, z; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> k; n     for (int j = 1; j <= k; j++) { n       cin >> z; n       a[i].push_back(z); n       cnt[z]++; n     } n   } n   long long ans = 0; n   for (int i = 1; i <= n; i++) { n     long long m = a[i].size(); n     long long temp = 0; n     for (int j = 0; j < m; j++) { n       temp += cnt[a[i][j]]; n     } n     long long cur = qmi((long long)n * n * m % mod, mod - 2) * temp % mod; n     ans = (cur + ans) % mod; n     ans %= mod; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Iinf = 1e9; n const long long LLinf = 1e18; n const double eps = 1e-6; n const long long mod = 1e9 + 7; n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(); n   cout.tie(); n   int s, x1, x2; n   cin >> s >> x1 >> x2; n   int t1, t2; n   cin >> t1 >> t2; n   int p, d; n   cin >> p >> d; n   if (d > 0) { n     if (x1 > x2) { n       cout << min(t1 * (s * 2 - p - x1 + abs(x1 - x2)), t2 * abs(x1 - x2)); n     } else if (p > x2) { n       cout << min(t1 * (s * 2 - p + x1 + abs(x1 - x2)), t2 * abs(x1 - x2)); n     } else if (p <= x1) { n       cout << min(t1 * (x1 - p + abs(x1 - x2)), t2 * abs(x1 - x2)); n     } else if (p > x1) { n       cout << min(t1 * (s * 2 - p + x1 + abs(x1 - x2)), t2 * abs(x1 - x2)); n     } n   } else { n     if (x1 > x2) { n       if (p >= x1) { n         cout << min(t1 * (p - x1 + abs(x1 - x2)), t2 * (x1 - x2)); n       } else { n         cout << min(t1 * (p + s + s - x1 + abs(x1 - x2)), t2 * abs(x1 - x2)); n       } n     } else { n       cout << min(t1 * (p + x1 + abs(x1 - x2)), t2 * abs(x1 - x2)); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int n = 1 << 17; n double sum[n * 2], lazy_mul[n * 2], lazy_add[n * 2]; n void push(int i, int l, int r) { n   if (lazy_mul[i] != 1 || lazy_add[i] != 0) { n     sum[i] = sum[i] * lazy_mul[i] + lazy_add[i] * (r - l); n     if (i < n) { n       for (int j : {i * 2, i * 2 + 1}) { n         lazy_mul[j] *= lazy_mul[i]; n         lazy_add[j] *= lazy_mul[i]; n         lazy_add[j] += lazy_add[i]; n       } n     } n     lazy_mul[i] = 1; n     lazy_add[i] = 0; n   } n } n void fma(double mul, double add, int a, int b, int l = 0, int r = n, n          int i = 1) { n   push(i, l, r); n   if (a >= r || b <= l) return; n   if (a <= l && b >= r) { n     lazy_mul[i] = mul; n     lazy_add[i] = add; n     push(i, l, r); n     return; n   } n   int m = l + r >> 1; n   fma(mul, add, a, b, l, m, i * 2); n   fma(mul, add, a, b, m, r, i * 2 + 1); n   sum[i] = sum[i * 2] + sum[i * 2 + 1]; n } n double querySum(int a, int b, int l = 0, int r = n, int i = 1) { n   push(i, l, r); n   if (a >= r || b <= l) return 0; n   if (a <= l && b >= r) { n     return sum[i]; n   } n   int m = l + r >> 1; n   return querySum(a, b, l, m, i * 2) + querySum(a, b, m, r, i * 2 + 1); n } n int main() { n   fill_n(lazy_mul, n * 2, 1.); n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n, q; n   cin >> n >> q; n   for (int i = 0; i < n; i++) { n     int a; n     cin >> a; n     fma(1, a, i, i + 1); n   } n   while (q--) { n     int type; n     cin >> type; n     if (type == 1) { n       int l1, r1, l2, r2; n       cin >> l1 >> r1 >> l2 >> r2; n       l1--, l2--; n       double e1 = querySum(l1, r1) / (r1 - l1); n       double e2 = querySum(l2, r2) / (r2 - l2); n       double p1 = 1. / (r1 - l1); n       fma(1 - p1, p1 * e2, l1, r1); n       double p2 = 1. / (r2 - l2); n       fma(1 - p2, p2 * e1, l2, r2); n     } else { n       int l, r; n       cin >> l >> r; n       l--; n       cout << setprecision(20) << querySum(l, r) <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n, m; n   cin >> n >> m; n   int arr[n]; n   int a = 0, b = 0; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n     if (arr[i] == 1) n       a++; n     else n       b++; n   } n   vector<pair<int, int>> v(m); n   for (int i = 0; i < m; i++) cin >> v[i].first >> v[i].second; n   for (int i = 0; i < m; i++) { n     if ((v[i].second - v[i].first + 1) % 2 == 1) n       cout <<  0  << endl; n     else { n       int c = v[i].second - v[i].first + 1; n       int d = c / 2; n       if (d <= a && d <= b) n         cout <<  1  << endl; n       else n         cout <<  0  << endl; n     } n   } n } n int main() { n   ios_base::sync_with_stdio(NULL); n   cin.tie(NULL); n   cout.tie(NULL); n   int t; n   t = 1; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, x, y, f[300010], a[300010][4]; n void dfs(int x) { n   int cnt = 0; n   for (int i = 1; i <= a[x][0]; i++) cnt += f[x] == f[a[x][i]]; n   if (cnt > 1) { n     f[x] ^= 1; n     for (int i = 1; i <= a[x][0]; i++) n       if (f[x] == f[a[x][i]]) dfs(a[x][i]); n   } n } n int main() { n   cin >> n >> m; n   while (m--) { n     cin >> x >> y; n     a[x][++a[x][0]] = y; n     a[y][++a[y][0]] = x; n   } n   for (int i = 1; i <= n; i++) dfs(i); n   for (int i = 1; i <= n; i++) cout << f[i]; n } n


#include <bits/stdc++.h> n using namespace std; n string str; n int main() { n   cin >> str; n   bool chk = false; n   for (int i = 0; i <= str.length(); i++) { n     for (char ch =  a ; ch <=  z ; ch++) { n       string temp1 = str.substr(0, i) + ch + str.substr(i); n       string temp2 = temp1; n       reverse(temp2.begin(), temp2.end()); n       if (temp1.compare(temp2) == 0) { n         cout << temp1 << endl; n         chk = true; n         break; n       } n     } n     if (chk == true) { n       break; n     } n   } n   if (chk == false) { n     cout <<  NA n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100010; n const int M = 55; n const int K = 200010; n const int LIT = 2500; n const int INF = 1 << 30; n const int dir[6][2] = {{2, 0}, {-2, 0}, {1, 1}, {-1, 1}, {1, -1}, {-1, -1}}; n bool check(long long x, long long y, long long n) { n   long long dist = x * x + 3 * y * y; n   long long cmp = 4 * n * n; n   return dist <= cmp; n } n bool inside(long long x, long long y, long long n) { n   for (int j = 0; j < 6; j++) { n     long long xx = x + dir[j][0]; n     long long yy = y + dir[j][1]; n     if (!check(xx, yy, n)) return 0; n   } n   return 1; n } n void solve(int n) { n   long long x = 0, y = 0; n   long long res = 1; n   while (1) { n     y += 2; n     if (!inside(x, y, n)) break; n     res += 2; n   } n   while (y >= 0) { n     x += 3, y++; n     while (y >= 0) { n       if (inside(x, y, n)) { n         res += (y + 1) * 2; n         break; n       } n       y -= 2; n     } n   } n   cout << res << endl; n } n int main() { n   int n; n   while (cin >> n) { n     solve(n); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100100; n long long n, x; n void printbin(long long a) { n   for (int i = 20; i >= 0; i--) { n     long long c = a & (1LL << i); n     cout << bool(c); n   } n   cout <<   n ; n } n int main() { n   cin >> n >> x; n   long long reversed = 0; n   reversed |= 1LL << 17; n   if (n == 1) { n     cout <<  YES n  << x; n     return 0; n   } n   if (n == 2 && x == 0) { n     cout <<  NO n ; n     return 0; n   } n   if (n == 2) { n     cout <<  YES n  << 0 <<     << x <<   n ; n     return 0; n   } n   cout <<  YES n ; n   long long value = 0; n   long long test = 0; n   for (int i = 1; i <= n - 3; i++) { n     cout << i <<    ; n     value ^= i; n     test ^= i; n   } n   reversed = x ^ value; n   reversed |= 1LL << 17; n   long long temp2 = 1LL << 17; n   if (reversed == 1LL << 17) { n     reversed |= 1LL << 18; n     long long temp = 1LL << 18; n     cout << reversed <<     << temp <<     << (temp ^ reversed) <<   n ; n   } else { n     cout << reversed <<     << temp2 <<     << 0 <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n namespace mist { n const int W = 103, N = W * W, M = 404; n int MOD; n long long inv(int x) { n   return (x == 1 || x == -1) ? x : (-(MOD / x) * inv(MOD % x) % MOD); n } n struct calculator { n   int s[M][M]; n   int n; n   void reset(int init_n) { n     n = init_n; n     memset(s, 0, sizeof(s)); n   } n   void add(int x, int y) { s[x][y]--, s[y][x]--, s[x][x]++, s[y][y]++; } n   int calc() { n     n--; n     for (int i = 1; i <= n; i++) n       for (int j = 1; j <= n; j++) s[i][j] %= MOD; n     int sign = 1, row = 1, drow; n     for (int col = 1; col <= n; col++) { n       for (drow = row; drow <= n && !s[drow][col]; drow++) n         ; n       if (drow > n) return 0; n       if (drow != row) { n         sign *= -1; n         for (int i = 1; i <= n; i++) std::swap(s[row][i], s[drow][i]); n       } n       for (int i = row + 1; i <= n; i++) n         if (s[i][col]) { n           int x = -s[i][col] * inv(s[row][col]) % MOD; n           for (int j = 1; j <= n; j++) n             s[i][j] = (s[i][j] + (long long)x * s[row][j]) % MOD; n         } n       row++; n     } n     int ret = 1; n     for (int i = 1; i <= n; i++) ret = (long long)ret * s[i][i] % MOD; n     ret *= sign; n     return (ret + MOD) % MOD; n   } n } C; n struct dus { n   int q[N]; n   void reset(int n) { n     for (int i = 1; i <= n; i++) q[i] = i; n   } n   int ask(int p) { return p == q[p] ? p : q[p] = ask(q[p]); } n   bool uni(int u, int v) { return ask(u) == ask(v); } n   bool link(int u, int v) { n     if (uni(u, v)) return 0; n     q[ask(u)] = ask(v); n     return 1; n   } n } d; n char G[W][W]; n int n, m; n int mp(int x, int y) { return x * (m + 1) + y; } n bool initialize() { n   scanf( %d%d%d , &n, &m, &MOD); n   for (int i = 1; i <= n; i++) scanf( %s , G[i] + 1); n   int total_number = (n + 1) * (m + 1); n   d.reset(total_number), d.reset(total_number); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) n       if (G[i][j] ==  / ) { n         int u = mp(i - 1, j), v = mp(i, j - 1), flag = 1; n         flag = d.link(u, v); n         if (!flag) return 0; n       } else if (G[i][j] ==     ) { n         int u = mp(i - 1, j - 1), v = mp(i, j), flag = 1; n         flag = d.link(u, v); n         if (!flag) return 0; n       } n   return 1; n } n int id[N], tot; n int part0() { n   tot = 0; n   for (int i = 0; i <= n; i++) n     for (int j = 0; j <= m; j++) n       if (!((i + j) & 1) && d.q[mp(i, j)] == mp(i, j)) id[mp(i, j)] = ++tot; n   C.reset(tot); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) n       if (G[i][j] ==  * ) { n         int u, v; n         if ((i + j) & 1) n           u = mp(i - 1, j), v = mp(i, j - 1); n         else n           u = mp(i, j), v = mp(i - 1, j - 1); n         u = id[d.ask(u)], v = id[d.ask(v)]; n         C.add(u, v); n       } n   return C.calc(); n } n int part1() { n   tot = 0; n   for (int i = 0; i <= n; i++) n     for (int j = 0; j <= m; j++) n       if (((i + j) & 1) && d.q[mp(i, j)] == mp(i, j)) id[mp(i, j)] = ++tot; n   C.reset(tot); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) n       if (G[i][j] ==  * ) { n         int u, v; n         if ((i + j) & 1) n           u = mp(i, j), v = mp(i - 1, j - 1); n         else n           u = mp(i - 1, j), v = mp(i, j - 1); n         u = id[d.ask(u)], v = id[d.ask(v)]; n         C.add(u, v); n       } n   return C.calc(); n } n void solve() { n   if (!initialize()) return void(printf( 0 n )); n   int ans = (part0() + part1()) % MOD; n   ans = (ans + MOD) % MOD; n   printf( %d n , ans); n } n }  // namespace mist n int main() { n   mist::solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5e2 + 5; n const int mod = 1e9 + 7; n const long long inf = 1e18; n int n, ans, pos, pos1; n int un[N], un1[N], cl[N], cl1[N], tr; n string t, s; n int calc(string e) { n   un[0] = cl[0] = 0, tr = 0; n   for (int i = 0; i < n; i++) { n     if (i) un[i] = un[i - 1], cl[i] = cl[i - 1]; n     if (e[i] ==  ( ) n       un[i]++; n     else { n       if (un[i]) n         un[i]--; n       else n         cl[i]++; n     } n   } n   if (cl[n - 1] == 0 and un[n - 1] == 0) tr = 1; n   un1[0] = cl1[0] = 0; n   for (int i = n - 1; i >= 0; i--) { n     un1[i] = un1[i + 1], cl1[i] = cl1[i + 1]; n     if (e[i] ==  ) ) n       un1[i]++; n     else { n       if (un1[i]) n         un1[i]--; n       else n         cl1[i]++; n     } n   } n   int jog = 0; n   for (int i = n - 1; i >= 1; i--) n     if (un[i - 1] == 0 and un1[i] == 0 and cl[i - 1] == cl1[i]) jog++; n   return jog + tr; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   cin >> n >> s; n   ans = calc(s); n   for (int i = 0; i < n; i++) { n     for (int j = i + 1; j < n; j++) { n       t = s; n       swap(t[i], t[j]); n       if (ans < calc(t)) { n         ans = calc(t); n         pos = i, pos1 = j; n       } n     } n   } n   cout << ans <<   n  << pos + 1 <<     << pos1 + 1; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXS = 100000; n inline long long get(long long n, long long ma, long long b) { n   return max(ma, (6 * n + b - 1) / b) * b; n } n int main() { n   long long n, a, b; n   cin >> n >> a >> b; n   if (a * b >= 6 * n) { n     cout << a * b << endl; n     cout << a <<     << b << endl; n   } else { n     long long best = b; n     for (int i = b; i <= MAXS; i++) n       if (get(n, a, best) > get(n, a, i)) best = i; n     long long best2 = a; n     for (int i = a; i <= MAXS; i++) n       if (get(n, b, best2) > get(n, b, i)) best2 = i; n     if (get(n, a, best) < get(n, b, best2)) { n       a = max(a, (6 * n + best - 1) / best); n       b = best; n     } else { n       a = best2; n       b = max(b, (6 * n + best2 - 1) / best2); n     } n     cout << a * b << endl; n     cout << a <<     << b << endl; n   } n } n


#include <bits/stdc++.h> n const int MAXN = 100; n int R[2 * MAXN + 2]; n int main() { n   int n, k, i; n   scanf( %d%d , &n, &k); n   for (i = 1; i <= 2 * n + 1; ++i) scanf( %d , R + i); n   for (i = 2; i <= 2 * n && k; ++i) n     if (R[i - 1] < R[i] - 1 && R[i] - 1 > R[i + 1]) { n       --R[i]; n       --k; n     } n   for (i = 1; i <= 2 * n + 1; ++i) printf( %d  , R[i]); n   putchar(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 300010; n pair<int, int> c[MAXN]; n int n, x[2]; n bool check(int pos, int fst, int snd) { n   int a = c[pos].first; n   int k = (x[fst] + a - 1) / a; n   if (pos + k >= n) { n     return false; n   } n   int val = c[pos + k].first; n   int dk = (x[snd] + val - 1) / val; n   if (pos + k + dk > n) { n     return false; n   } n   if (fst == 0) { n     printf( Yes n ); n     printf( %d %d n , k, dk); n     for (int i = 0; i < k; ++i) { n       printf( %d  , c[pos + i].second + 1); n     } n     printf(  n ); n     for (int i = 0; i < dk; ++i) { n       printf( %d  , c[pos + k + i].second + 1); n     } n     printf(  n ); n   } else { n     printf( Yes n ); n     printf( %d %d n , dk, k); n     for (int i = 0; i < dk; ++i) { n       printf( %d  , c[pos + k + i].second + 1); n     } n     printf(  n ); n     for (int i = 0; i < k; ++i) { n       printf( %d  , c[pos + i].second + 1); n     } n     printf(  n ); n   } n   return true; n } n int main() { n   scanf( %d%d%d , &n, &x[0], &x[1]); n   for (int i = 0; i < n; ++i) { n     scanf( %d , &c[i].first); n     c[i].second = i; n   } n   sort(c, c + n); n   for (int i = 0; i < n; ++i) { n     if (check(i, 0, 1) || check(i, 1, 0)) { n       return 0; n     } n   } n   printf( No n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s; n int n, m, ans = 0; n int main() { n   cin >> n >> m; n   while (n--) { n     cin >> s; n   } n   int flag = 0; n   for (int i = 0; i < s.size(); ++i) { n     if (s[i] ==  B ) { n       flag = 1; n     } else if (flag) { n       flag = 0; n       ans++; n     } n   } n   cout << ans + flag <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long x2, y2; n long long euler(long long a, long long b) { n   if (b == 0) { n     x2 = 1; n     y2 = 0; n     return a; n   } else { n     long long res = euler(b, a % b); n     long long temp = x2; n     x2 = y2; n     y2 = temp - (a / b) * y2; n     return res; n   } n } n int main() { n   int T; n   long long x, y, m, n, vx, vy; n   long long k1, k2; n   long long res, res1, res2, xx, yy; n   cin >> n >> m >> x >> y >> vx >> vy; n   if (!vx) { n     if ((!x) || (x == n)) { n       cout << x <<     << (vy > 0 ? m : 0) << endl; n     } else n       cout <<  -1  << endl; n     return 0; n   } n   if (!vy) { n     if ((!y) || (y == m)) { n       cout << (vx > 0 ? n : 0) <<     << y << endl; n     } else n       cout <<  -1  << endl; n     return 0; n   } n   k1 = (vx == 1 ? ((n - x) % n) : (x % n)); n   k2 = (vy == 1 ? ((m - y) % m) : (y % m)); n   long long g = euler(n, m); n   if (abs(k1 - k2) % g) { n     cout <<  -1  << endl; n     return 0; n   } n   long long lcm = m * n / g; n   res1 = (k2 * x2) % m; n   res1 = res1 * (n / g); n   res2 = (k1 * y2) % n; n   res2 = res2 * (m / g); n   res = (res1 + res2) % lcm; n   res = (res + lcm) % lcm; n   xx = x + res * vx; n   yy = y + res * vy; n   cout << ((xx % (2 * n)) + 2 * n) % (2 * n) <<     n        << ((yy % (2 * m)) + 2 * m) % (2 * m) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n const int MAX = 1e5 + 5; n int n, s, h, m; n vector<pair<int, int> > a; n int main(int argc, char const *argv[]) { n   ios_base::sync_with_stdio(false); n   cin >> n >> s; n   for (int i = 0; i < n; i++) { n     cin >> h >> m; n     a.push_back(make_pair(h, m)); n   } n   bool done = false; n   if (a[0].first * 60 + a[0].second >= s + 1) n     cout <<  0 0 ; n   else { n     for (int i = 0; i < n - 1; i++) { n       int gap = n           (a[i + 1].first - a[i].first) * 60 + (a[i + 1].second - a[i].second); n       if (gap >= 2 * (s + 1)) { n         done = true; n         if (a[i].second + s + 1 >= 60) { n           int mins = s + 1 - (60 - a[i].second); n           if (mins == 60) n             cout << a[i].first + 2 <<   0 ; n           else n             cout << a[i].first + 1 <<     << mins; n         } else n           cout << a[i].first <<     << a[i].second + s + 1; n         break; n       } n     } n     if (!done) { n       if (a[n - 1].second + s + 1 >= 60) { n         int mins = s + 1 - (60 - a[n - 1].second); n         if (mins == 60) n           cout << a[n - 1].first + 2 <<   0 ; n         else n           cout << a[n - 1].first + 1 <<     << mins; n       } else n         cout << a[n - 1].first <<     << a[n - 1].second + s + 1; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC target( avx2 ) n #pragma GCC optimization( O3 ) n #pragma GCC optimization( unroll-loops ) n using namespace std; n template <class T> n using vec = std::vector<T>; n bool __hack = std::ios::sync_with_stdio(false); n auto __hack1 = cin.tie(nullptr); n struct Input { n   Input(istream &in) : in(&in) {} n   template <class T> n   T next() const { n     T x; n     *in >> x; n     return x; n   } n   int ni() const { return next<int>(); } n   istream *in; n }; n Input in(cin); n class Output { n  private: n   ostream *out; n   template <typename T> n   void printSingle(const T &value) { n     *out << value; n   } n  n  public: n   Output(ostream &out) : out(&out) {} n   inline void print() {} n   template <typename T, typename... Ts> n   inline void print(const T &f, const Ts &...args) { n     printSingle(f); n     if (sizeof...(args) != 0) { n       *out <<    ; n       print(args...); n     } n   } n   template <typename... Ts> n   inline void println(const Ts &...args) { n     print(args...); n     *out <<   n ; n   } n   template <typename... Ts> n   inline void operator()(const Ts &...args) { n     println(args...); n   } n   template <typename T> n   ostream &operator<<(const T &x) { n     *out << x; n     return *out; n   } n }; n Output out(cout); n namespace template_util { n constexpr int bytecount(uint64_t x) { return x ? 1 + bytecount(x >> 8) : 0; } n template <int N> n struct bytetype {}; n template <uint64_t N> n struct minimal_uint : bytetype<bytecount(N)> {}; n }  // namespace template_util n void solveOne() { n   auto n = in.ni(); n   auto m = in.ni(); n   vec<pair<int, int>> edges(m); n   for (int i = 0; i < (m); ++i) { n     auto x = in.ni() - 1; n     auto y = in.ni() - 1; n     if (x > y) swap(x, y); n     edges[i] = {x, y}; n   } n   vec<int> used(3 * n, 0); n   vec<int> takenEdges; n   for (int i = 0; i < (m); ++i) { n     if (!used[edges[i].first] && !used[edges[i].second]) { n       takenEdges.push_back(i); n       used[edges[i].first] = true; n       used[edges[i].second] = true; n     } n     if (takenEdges.size() == n) { n       out( Matching ); n       for (const auto &e : takenEdges) { n         out << e + 1 <<    ; n       } n       out(); n       return; n     } n   } n   out( IndSet ); n   int cnt = 0; n   for (int i = 0; i < (3 * n); ++i) { n     if (!used[i]) { n       out << i + 1 <<    ; n       ++cnt; n     } n     if (cnt == n) break; n   } n   out(); n } n void solve(istream &inStream, ostream &outStream) { n   in = Input(inStream); n   out = Output(outStream); n   auto tests = in.ni(); n   while (tests--) { n     solveOne(); n   } n } n int main() { n   solve(cin, cout); n   return 0; n } n