#include<bits/stdc++.h> n #define rep(i,a,b) for(int i=(a);i<(b);++i) n #define per(i,a,b) for(int i=(b)-1;i>=(a);--i) n #define ll long long n #define lb(x) ((x)&-(x)) n #define pii pair<int,int> n #define vi vector<int> n #define pb push_back n #define fi first n #define se second n #define de(x) cout<<#x<<  x <<endl n #define LCAFA rep(i,1,20)rep(j,1,n+1)fa[j][i]=fa[fa[j][i-1]][i-1] n #define all(x) x.begin(),x.end() n #define ls(x) x<<1 n #define rs(x) x<<1|1 n #define pr(x) {for(auto v:x)cout<<v<<   ;cout<<#x<<endl;} n using namespace std; n const int N=3e4+9; n const ll mod=998244353; n const ll Inf=1e18; n /* n inline int add(int a,const int &b){ n  a+=b; n  if(a>=mod)a-=mod; n  return a; n } n inline int sub(int a,const int &b){ n  a-=b; n  if(a<0)a+=mod; n  return a; n } n inline int mul(const int &a,const int &b){return 1ll*a*b%mod;} n int jie[N],inv[N]; n ll quick(ll a,ll b){ n  ll res=1; n  while(b){ n   if(b&1)res=res*a%mod; n   a=a*a%mod; n   b>>=1; n  } n  return res; n } n inline int C(int m,int n){ n  if(n>m||m<0||n<0)return 0; n  return mul(mul(jie[m],inv[n]),inv[m-n]); n } n void pre(){ n  inv[0]=inv[1]=1,jie[0]=1; n  rep(i,2,N)inv[i]=-1ll*mod/i*inv[mod%i]%mod+mod; n  rep(i,1,N)inv[i]=mul(inv[i],inv[i-1]),jie[i]=mul(jie[i-1],i); n } n */ n ll dp[N][5]; n int p[N]; n vi s[N]; n ll L[N][5][16],R[N][5][16]; n int main(){ n  //pre(); n  int T; n  scanf( %d ,&T); n  while(T--){ n   int n,c,q; n   scanf( %d%d%d ,&n,&c,&q); n   rep(i,0,n)scanf( %d ,&p[i]); n   rep(i,0,c+1)dp[n][i]=1; n   per(i,0,n){ n    rep(j,0,c+1){ n     dp[i][j]=0; n     rep(k,0,j+1){ n      if(i+k<n)dp[i][j]+=dp[i+k+1][j-k]; n     } n    } n   } n   rep(i,0,n){ n    s[i].clear(); n    rep(j,0,c+1){ n     if(i+j<n)s[i].pb(j); n    } n    sort(all(s[i]),[&](int a,int b){return p[i+a]<p[i+b];}); n   } n   int lim=0; n   while((1<<lim)<n)++lim; n   rep(i,0,n)rep(j,0,c+1)rep(k,0,lim+1)L[i][j][k]=R[i][j][k]=0; n   per(i,0,n){ n    rep(j,0,c+1){ n     if(i+j>=n)break; n     ll sum=0; n     for(auto v:s[i]){ n      if(v>j)continue; n      if(v==0)break; n      sum+=dp[i+v+1][j-v]; n     } n     L[i][j][0]=sum; n     R[i][j][0]=sum+dp[i+1][j]; n     rep(k,1,lim+1){ n      if(i+(1<<k)<=n){ n       L[i][j][k]=L[i][j][k-1]+L[i+(1<<k-1)][j][k-1]; n       R[i][j][k]=L[i][j][k-1]+R[i+(1<<k-1)][j][k-1]; n      } n     } n    } n   } n   while(q--){ n    int pos; n    ll id; n    scanf( %d%lld ,&pos,&id); n    if(id>dp[0][c]){ n     puts( -1 ); n     continue; n    } n    --pos; n    --id; n    int i=0,ans=p[pos],r=c; n   // cout<<ans<< ans n ; n    int cnt=0; n    while(i<n){ n     ++cnt; n     if(cnt>6)return 0; n     for(int j=lim;j>=0;--j){ n      if(i+(1<<j)<=n&&L[i][r][j]<=id&&id<R[i][r][j]){ n       id-=L[i][r][j]; n       i+=1<<j; n      } n     } n     if(i==n)break; n    // cout<<cnt<<   <<i<<   <<r<<   <<id<< i n ; n     for(auto v:s[i]){ n      if(v>r)continue; n      if(id>=dp[i+v+1][r-v]){ n       id-=dp[i+v+1][r-v]; n      } n      else{ n      // cout<<v<< v n ; n       int i1=i+v; n       r-=v; n       if(i<=pos&&pos<=i1){ n        ans=p[i+i1-pos]; n       } n       i=i1+1; n       break; n      } n     } n    } n     printf( %d n ,ans); n   } n  }  n } n /* n 1 n 3 1 1 n 1 2 3 n 2 1 n  n 1 n 6 4 1 n 6 5 4 3 1 2 n 3 14 n  n 1 n 20 4 1 n 16 5 15 14 13 3 17 18 2 20 19 6 4 1 12 8 11 10 9 7 n 8 1100 n */ n


#include <bits/stdc++.h> n using namespace std; n vector<vector<int> > al, dist; n int n, m; n bool done[5000]; n vector<int> bfs(const vector<vector<int> >& al, int s) { n   queue<int> q; n   vector<int> dist(al.size(), INT_MAX); n   dist[s] = 0; n   q.push(s); n   while (!q.empty()) { n     int i = q.front(); n     q.pop(); n     for (int j : al[i]) n       if (dist[j] == INT_MAX) { n         dist[j] = dist[i] + 1; n         q.push(j); n       } n   } n   return dist; n } n int main() { n   ios::sync_with_stdio(0); n   cin >> n >> m; n   int r = n; n   al.resize(n); n   dist.resize(n); n   int i, j; n   for (int c = 0; c < m; c++) { n     cin >> i >> j; n     i--; n     j--; n     al[i].push_back(j); n   } n   for (int s = 0; s < n; s++) dist[s] = bfs(al, s); n   for (int i = 0; i < n; i++) n     if (!done[i]) { n       vector<int> comp{i}; n       done[i] = 1; n       bool outgo = 0; n       for (int j = 0; j < n; j++) n         if (dist[i][j] != INT_MAX && i != j) { n           if (dist[j][i] != INT_MAX) { n             comp.push_back(j); n             done[j] = 1; n           } else n             outgo = 1; n         } n       if (!outgo && comp.size() > 1) { n         int m = INT_MAX; n         for (int c = 0; c < comp.size(); c++) n           for (int d = 0; d < comp.size(); d++) n             if (c != d) n               m = min(m, dist[comp[c]][comp[d]] + dist[comp[d]][comp[c]]); n         r += 998 * m + 1; n       } n     } n   cout << r <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 3006; n struct arr { n   int x, y, i; n } a[maxn]; n int r, c, n, K; n int pos[maxn], L[maxn], R[maxn]; n long long ans; n bool cmpx(arr x, arr y) { n   if (x.x == y.x) return x.y < y.y; n   return x.x < y.x; n } n bool cmpy(arr x, arr y) { n   if (x.y == y.y) return x.x < y.x; n   return x.y < y.y; n } n int main() { n   scanf( %d%d%d%d , &r, &c, &n, &K); n   for (int i = 1; i <= n; i++) scanf( %d%d , &a[i].x, &a[i].y); n   sort(a + 1, a + n + 1, cmpy); n   for (int i = 1; i <= n; i++) a[i].i = i; n   sort(a + 1, a + n + 1, cmpx); n   for (int i = 1; i <= n; i++) L[i] = i - 1; n   for (int i = 1; i <= n; i++) R[i] = i + 1; n   for (int i = 1; i <= n; i++) pos[a[i].i] = i; n   R[0] = 1; n   L[n + 1] = n; n   pos[n + 1] = n + 1; n   a[n + 1].x = r + 1; n   a[n + 1].y = c + 1; n   int nn = n; n   for (int i = r; i >= 1; i--) { n     int t = 0; n     long long tmp = 0; n     for (int j = 1; j <= n && a[j].x <= i; t = j++) n       R[L[a[j].i]] = R[a[j].i], L[R[a[j].i]] = L[a[j].i]; n     for (int j = i, k = t; j >= 1; ans += tmp, j--) { n       for (; a[k].x == j; k--) { n         R[L[a[k].i]] = a[k].i; n         L[R[a[k].i]] = a[k].i; n         int val = 1, lp = a[k].i, rp = a[k].i; n         for (; L[lp] && val < K; lp = L[lp], val++) n           ; n         for (; lp <= a[k].i; lp = R[lp], val--) { n           for (; R[rp] <= n && val < K; rp = R[rp], val++) n             ; n           if (val < K) break; n           long long tmp_1 = a[pos[lp]].y - a[pos[L[lp]]].y; n           long long tmp_2 = a[pos[R[rp]]].y - a[pos[rp]].y; n           tmp += tmp_1 * tmp_2; n         } n       } n     } n     while (a[nn].x == i) { n       R[L[a[nn].i]] = R[a[nn].i]; n       L[R[a[nn].i]] = L[a[nn].i]; n       nn--; n     } n   } n   printf( %lld n , ans); n   return 0; n } n


/*Allah Vorosha*/ n #include<bits/stdc++.h> n #define ll long long n #define ld long double n #define pb push_back n #define in insert n #define rev reverse n #define all(x) (x).begin(),(x).end() n #define all2(x) (x).rbegin(),(x).rend() n #define sz(v) (int)v.size() n #define yes cout <<  YES n  n #define no cout <<  NO n ; n #define take for(auto &it : a) cin >> it; n #define vi vector<int> n #define vii vector<pair<int, int > > n #define vl vector<ll> n #define fi first n #define se second n #define l_b lower_bound n #define u_b upper_bound n #define ios ios_base::sync_with_stdio(0);cin.tie();cout.tie(); n #define tail cout <<   n  n  n using namespace std; n  n template<class T> void read(T &x) { cin >> x; } n template<class T, class ...U> void read(T &x, U& ... u) { read(x); read(u...); } n template<class T> void print1(const T &x) { cout << x <<   n ; } n template<class T, class ...U> void print1(const T &x, const U& ... u) { print1(x); print1(u...); } n template<class T> void print2(const T &x) { cout << x <<    ; } n template<class T, class ...U> void print2(const T &x, const U& ... u) { print2(x); print2(u...); } n  n int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0}; n //int n, m; n  n bool valid (int x, int y) { n     //return (x >= 0 && x < n && y >= 0 && y < m); n } n  n bool vis[55][55]; n  n const int N = 1005, mod = 1e9 + 7, M = 5e5 + 5; n const ll inf = 9e18; n int cs = 1; n  n void solve() { n     ll n, c; n     read(n, c); n     vi a(n); n     take; n     vi b(n - 1); n     for(auto &it : b) n         cin >> it; n     ll hi = c, low = 1, mid, ans; n     while(hi >= low) { n         mid = (hi + low) >> 1; n         ll day = mid; n         ll tk = 0; n         bool ok = 0; n         for(int i = 0; i < n; i++) { n             if(tk + a[i] * day >= c) { n                 ok = 1; break; n             } n             else { n                 if(i == n - 1) { n                     if(tk + a[i] * day >= c) { n                         ok = 1; n                         break; n                     } n                 } n                 else { n                     if(tk >= b[i]) { n                         tk -= b[i]; n                         day--; n                     } n                     else { n                         ll k = b[i] - tk; n                         ll l = k / a[i] + (k % a[i] != 0); n                         day -= l + 1; n                         tk += l * a[i]; n                         tk -= b[i]; n                     } n                 } n             } n             //if(mid == 6) print1(day); n             if(day < 0) { n                 break; n             } n         } n         if(ok) { n             ans = mid; n             hi = mid - 1; n         } n         else { n             low = mid + 1; n         } n     } n     cout << ans <<   n ; n } n  n int main() { n     ios; n     int tc; n     read(tc); n     while(tc--) n     solve(); n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 1; n long long a[N]; n int main() { n   ios_base::sync_with_stdio(0); n   int n; n   cin >> n; n   priority_queue<pair<long long, int>, vector<pair<long long, int>>, n                  greater<pair<long long, int>>> n       q; n   for (int i = 1; i <= n; ++i) { n     int x; n     cin >> x; n     q.push({x, i}); n   } n   long long ans(0); n   while (!q.empty()) { n     pair<long long, int> p = q.top(); n     q.pop(); n     if (q.top().first == p.first && !q.empty()) { n       int x = q.top().second; n       q.pop(); n       q.push({p.first * 2, x}); n     } else { n       ans++; n       a[p.second] = p.first; n     } n   } n   cout << ans << endl; n   for (int i = 1; i <= n; ++i) n     if (a[i] != 0) cout << a[i] <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1001000; n const int INF = 1e9; n int n, L, U; n int head[maxn], nxt[maxn << 1], rdc[maxn << 1], w[maxn << 1], tnt; n int prt, rt, asiz; n int siz[maxn], mdep[maxn]; n int fdep, gdep; n int f[maxn], g[maxn], fp[maxn], gp[maxn], dta; n int q[maxn], qt, qe; n int ansu, ansv; n vector<int> v[maxn], t[maxn]; n bool vis[maxn]; n inline int read() { n   int res = 0; n   char ch = getchar(), ch1 = ch; n   while (!isdigit(ch)) ch1 = ch, ch = getchar(); n   while (isdigit(ch)) res = (res << 3) + (res << 1) + ch -  0 , ch = getchar(); n   return ch1 ==  -  ? -res : res; n } n bool cmp(int x, int y) { return mdep[rdc[x]] < mdep[rdc[y]]; } n void add(int x, int y, int z) { n   nxt[++tnt] = head[x]; n   head[x] = tnt; n   rdc[tnt] = y; n   w[tnt] = z; n } n void findrt(int p, int f) { n   int res = 0; n   siz[p] = 1; n   for (register int i = head[p]; i; i = nxt[i]) n     if (rdc[i] != f && !vis[rdc[i]]) { n       findrt(rdc[i], p); n       res = max(res, siz[rdc[i]]); n       siz[p] += siz[rdc[i]]; n     } n   res = max(res, asiz - siz[p]); n   if (res <= (asiz >> 1)) rt = p; n } n void builddfs(int p, int f) { n   siz[p] = 1; n   for (register int i = head[p]; i; i = nxt[i]) n     if (rdc[i] != f && !vis[rdc[i]]) { n       mdep[rdc[i]] = mdep[p] + 1; n       builddfs(rdc[i], p); n       siz[p] += siz[rdc[i]]; n     } n   for (register int i = head[p]; i; i = nxt[i]) n     if (rdc[i] != f && !vis[rdc[i]]) mdep[p] = max(mdep[p], mdep[rdc[i]]); n } n void build(int p) { n   vis[p] = true; n   mdep[p] = 0; n   for (register int i = head[p]; i; i = nxt[i]) { n     if (vis[rdc[i]]) continue; n     mdep[rdc[i]] = 1; n     builddfs(rdc[i], p); n     mdep[p] = max(mdep[p], mdep[rdc[i]]); n     v[p].push_back(i); n   } n   sort(v[p].begin(), v[p].end(), cmp); n   for (register int i = head[p]; i; i = nxt[i]) { n     if (vis[rdc[i]]) continue; n     asiz = siz[rdc[i]]; n     findrt(rdc[i], p); n     t[p].push_back(rt); n     build(rt); n   } n   vis[p] = false; n } n void dfs(int p, int f, int dep, int pv) { n   if (g[dep] < pv) { n     g[dep] = pv; n     gp[dep] = p; n   } n   for (register int i = head[p]; i; i = nxt[i]) n     if (rdc[i] != f && !vis[rdc[i]]) n       dfs(rdc[i], p, dep + 1, pv + (w[i] >= dta ? 1 : -1)); n   gdep = max(gdep, dep); n } n bool check(int p) { n   if ((mdep[p] << 1) < L) return false; n   bool flag = false; n   vis[p] = true; n   fp[0] = gp[0] = p; n   for (register int i = 0; i < v[p].size() && !flag; ++i) { n     dfs(rdc[v[p][i]], p, 1, (w[v[p][i]] >= dta ? 1 : -1)); n     qt = 1; n     qe = 0; n     for (register int j = 0, k = min(fdep, U); j <= gdep; ++j) { n       while (qt <= qe && q[qt] + j > U) ++qt; n       while (j + k >= L && k >= 0) { n         while (qt <= qe && f[q[qe]] <= f[k]) --qe; n         q[++qe] = k; n         --k; n       } n       if (qt <= qe && g[j] + f[q[qt]] >= 0) { n         flag = true; n         ansu = gp[j]; n         ansv = fp[q[qt]]; n         break; n       } n     } n     fdep = gdep; n     while (gdep) { n       if (f[gdep] < g[gdep]) { n         f[gdep] = g[gdep]; n         fp[gdep] = gp[gdep]; n       } n       g[gdep] = -INF; n       --gdep; n     } n   } n   while (fdep) { n     f[fdep] = -INF; n     --fdep; n   } n   if (flag) { n     vis[p] = false; n     return true; n   } n   for (register int i = 0; i < t[p].size(); ++i) n     if (check(t[p][i])) { n       vis[p] = false; n       return true; n     } n   vis[p] = false; n   return false; n } n int main() { n   n = read(); n   L = read(); n   U = read(); n   for (int i = 1, x, y, z; i < n; ++i) { n     x = read(); n     y = read(); n     z = read(); n     add(x, y, z); n     add(y, x, z); n   } n   asiz = n; n   findrt(1, 0); n   build(prt = rt); n   for (register int i = 0; i <= n; ++i) f[i] = g[i] = -INF; n   f[0] = g[0] = 0; n   int l = -1, r = 1e9; n   while (l < r) { n     dta = (l + r + 1) >> 1; n     if (check(prt)) n       l = dta; n     else n       r = dta - 1; n   } n   dta = l; n   check(l); n   printf( %d %d n , ansu, ansv); n   return 0; n } n


#include <bits/stdc++.h> n const double pi = acos(-1.0); n const double eps = 1e-9; n using namespace std; n int k, n1, n2, n3, t1, t2, t3; n void solve() { n   cin >> k >> n1 >> n2 >> n3 >> t1 >> t2 >> t3; n   set<pair<long long, int>> st[3]; n   for (int i = 1; i <= 1000; i++) { n     if (i <= n1) { n       st[0].insert({0, i}); n     } n     if (i <= n2) { n       st[1].insert({0, i}); n     } n     if (i <= n3) { n       st[2].insert({0, i}); n     } n   } n   long long ans = 0; n   while (k--) { n     auto beg0 = *st[0].begin(); n     auto beg1 = *st[1].begin(); n     auto beg2 = *st[2].begin(); n     auto last0 = beg0, last1 = beg1, last2 = beg2; n     st[0].erase(beg0); n     st[1].erase(beg1); n     st[2].erase(beg2); n     beg0.first += t1; n     beg1.first = max(beg1.first + t2, beg0.first + t2); n     beg2.first = max(beg2.first + t3, beg1.first + t3); n     ans = max(ans, beg2.first); n     beg1.first = beg2.first - t3; n     beg0.first = beg1.first - t2; n     assert(last0.first <= beg0.first); n     assert(last1.first <= beg1.first); n     assert(last2.first <= beg2.first); n     st[0].insert(beg0); n     st[1].insert(beg1); n     st[2].insert(beg2); n   } n   cout << ans << endl; n } n int main() { n   ios::sync_with_stdio(NULL), cin.tie(0), cout.tie(0); n   cout.setf(ios::fixed), cout.precision(7); n   int step = 1; n   for (int i = 1; i <= step; i++) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long read() { n   long long ans = 0, f = 1; n   char c = getchar(); n   while (c >  9  || c <  0 ) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     ans = ans * 10 + c -  0 ; n     c = getchar(); n   } n   return ans * f; n } n const long long N = 2e5 + 5; n long long t, n, m, k, x[N], y[N], ans, mmax[N]; n signed main() { n   t = read(); n   while (t--) { n     n = read(); n     k = read(); n     for (long long i = 1; i <= n; ++i) x[i] = read(); n     for (long long i = 1; i <= n; ++i) y[i] = read(); n     sort(x + 1, x + 1 + n); n     long long now = 0; n     ans = 0; n     for (long long i = 1; i <= n; ++i) { n       long long len = upper_bound(x + 1, x + 1 + n, x[i] + k) - x - 1; n       now = max(now, mmax[i]); n       ans = max(ans, len - i + 1 + now); n       mmax[len + 1] = max(mmax[len + 1], len - i + 1); n     } n     for (long long i = 1; i <= n + 1; ++i) mmax[i] = 0; n     printf( %lld n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int dx[4] = {0, 0, -1, 1}; n const int dy[4] = {-1, 1, 0, 0}; n int i, j, x, y, n, m, e[26], b[510][510], l, ans, flag, xx, yy; n char c, s[100010]; n int main() { n   scanf( %d%d%d%d , &n, &m, &x, &y); n   scanf(  %s , s); n   l = strlen(s); n   ans = 1; n   printf( 1 ); n   e[ L  -  A ] = 0; n   e[ R  -  A ] = 1; n   e[ U  -  A ] = 2; n   e[ D  -  A ] = 3; n   memset(b, 0, sizeof(b)); n   b[x][y] = 1; n   flag = 0; n   for (i = 0; i < l; i++) { n     xx = x + dx[e[s[i] -  A ]]; n     yy = y + dy[e[s[i] -  A ]]; n     if (flag || xx < 1 || xx > n || yy < 1 || yy > m) { n       if (i == l - 1) { n         printf(  %d n , n * m - ans); n         continue; n       } n       printf(  0 ); n       continue; n     } n     x = xx; n     y = yy; n     if (b[xx][yy] == 1) { n       if (i == l - 1) { n         printf(  %d n , n * m - ans); n         continue; n       } n       printf(  0 ); n       continue; n     } n     ans++; n     b[x][y] = 1; n     if (i == l - 1) { n       printf(  %d n , n * m + 1 - ans); n       continue; n     } n     printf(  1 ); n   } n   puts(  ); n   return 0; n } n


#include<iostream> n #include<cstdio> n #include<cstring> n #include<algorithm> n #include<vector> n using namespace std; n int n,m; n char s[200009],t[200009]; n int f[200009],g[200009]; n vector<int> p[30]; n int main() n { n  scanf( %d%d ,&n,&m); n  scanf( %s%s ,s+1,t+1); n  for(int i=1;i<=n;i++) n   s[i]-= a -1; n  for(int i=1;i<=m;i++) n   t[i]-= a -1; n  for(int i=1;i<=n;i++) n   p[s[i]].push_back(i); n  for(int i=1;i<=m;i++) n   f[i]=*upper_bound(p[t[i]].begin(),p[t[i]].end(),f[i-1]); n  g[m+1]=n+1; n  for(int i=m;i>=1;i--) n   g[i]=*(lower_bound(p[t[i]].begin(),p[t[i]].end(),g[i+1])-1); n  int ans=0; n  for(int i=1;i<=m-1;i++) n   ans=max(ans,g[i+1]-f[i]); n  printf( %d ,ans); n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long in, temp = 0, t = 0; n   int casos; n   cin >> casos; n   for (int i = 0; i < casos; i++) { n     cin >> in; n     temp = in * (in + 1) / 2; n     for (long long i = 0;; i++) { n       if ((1 << i) > in) { n         break; n       } else { n         long long j = 1 << i; n         t = t + j; n       } n     } n     temp = temp - 2 * t; n     cout << temp << endl; n     t = 0; n     temp = 0; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 1000000007; n const int N = 105; n int n, m, k, nl, d[N][N], ok; n struct point { n   int x, y; n   point(){}; n   point(int x, int y) : x(x), y(y) {} n }; n string a[N]; n vector<point> l[N * N]; n int s[N * N], p[N * N]; n void input() { n   cin >> n >> m >> k; n   getline(cin, a[0]); n   for (int i = 0; i < n; i++) { n     getline(cin, a[i]); n   } n } n int vx[] = {0, 1, 0, -1}; n int vy[] = {-1, 0, 1, 0}; n int check(int x, int y) { return x >= 0 && x < n && y >= 0 && y < m; } n void dfs(int x, int y) { n   l[nl].push_back(point(x, y)); n   s[nl]++; n   d[x][y] = 1; n   for (int k = 0; k < 4; k++) { n     int u = x + vx[k]; n     int v = y + vy[k]; n     if (!check(u, v)) { n       ok = 0; n       continue; n     } n     if (a[u][v] !=  .  || d[u][v]) continue; n     dfs(u, v); n   } n } n int cmp(int x, int y) { return s[x] < s[y]; } n void solve() { n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (a[i][j] ==  .  && !d[i][j]) { n         l[nl].clear(); n         s[nl] = 0; n         p[nl] = nl; n         ok = 1; n         dfs(i, j); n         if (ok) nl++; n       } n     } n   } n   sort(p, p + nl, cmp); n   int ans = 0; n   for (int i = 0; i < nl - k; i++) { n     ans += s[p[i]]; n     for (int j = 0; j < l[p[i]].size(); j++) { n       int u = l[p[i]][j].x; n       int v = l[p[i]][j].y; n       a[u][v] =  * ; n     } n   } n   cout << ans << endl; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       cout << a[i][j]; n     } n     cout << endl; n   } n } n int main() { n   int test = 1; n   while (test--) { n     input(); n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long fpm(long long b, long long e, long long m) { n   long long t = 1; n   for (; e; e >>= 1, b = b * b % m) e & 1 ? t = t * b % m : 0; n   return t; n } n template <class T> n inline bool chkmin(T &a, T b) { n   return a > b ? a = b, true : false; n } n template <class T> n inline bool chkmax(T &a, T b) { n   return a < b ? a = b, true : false; n } n template <class T> n inline T sqr(T x) { n   return x * x; n } n template <class edge> n struct Graph { n   vector<vector<edge> > adj; n   Graph(int n) { n     adj.clear(); n     adj.resize(n + 5); n   } n   Graph() { adj.clear(); } n   void resize(int n) { adj.resize(n + 5); } n   void add(int s, edge e) { adj[s].push_back(e); } n   void del(int s, edge e) { n     adj[s].erase(find((adj[s]).begin(), (adj[s]).end(), e)); n   } n   int deg(int v) { return adj[v].size(); } n   vector<edge> &operator[](int t) { return adj[t]; } n }; n struct crd { n   long double x, y, z; n   void read() { cin >> x >> y >> z; } n   void out() { cerr <<  (  << x <<  ,   << y <<  ,   << z <<  ) ; } n   crd operator^(crd &w) { n     return (crd){y * w.z - w.y * z, z * w.x - w.z * x, x * w.y - y * w.x}; n   } n   long double operator*(crd &w) { return x * w.x + y * w.y + z * w.z; } n   crd operator+(crd &w) { return (crd){x + w.x, y + w.y, z + w.z}; } n   crd operator-(crd &w) { return (crd){x - w.x, y - w.y, z - w.z}; } n   crd operator*(long double &w) { return (crd){x * w, y * w, z * w}; } n   long double len() { return sqrt(x * x + y * y + z * z); } n }; n crd A, p, O, v; n long double R, r; n long double sphere(crd o, long double r) { n   o = o - A; n   crd plane = o ^ v; n   crd vert = v ^ plane; n   long double mindis = o * vert / vert.len(); n   if (mindis > R + r || o * v < 0) return 1e300; n   o.out(), vert.out(), plane.out(), ({}); n   long double dist = o.len(); n   long double run = n       sqrt(sqr(dist) - sqr(mindis)) - sqrt(sqr(R + r) - sqr(mindis)); n   return run / v.len(); n } n int main() { n   ios_base::sync_with_stdio(false); n   long double ans = 1e300; n   A.read(), v.read(); n   v.out(), cerr << endl; n   int n, m; n   cin >> R >> n; n   for (int i = 1; i <= n; ++i) { n     O.read(); n     cin >> r >> m; n     ans = min(ans, sphere(O, r)); n     for (; m--;) p.read(), ans = min(ans, sphere(O + p, 0)); n   } n   cout << setprecision(15) << (ans > 1e200 ? -1 : ans) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void ioe() { n   freopen( in_3.txt ,  r , stdin); n   freopen( out_3.txt ,  w , stdout); n   freopen( er_3.txt ,  w , stderr); n } n struct lex_compare { n  public: n   bool operator()(const pair<long long, long long>& lhs, n                   const pair<long long, long long>& rhs) const { n     return (lhs.second > rhs.second); n   } n }; n long long pw(long long a, long long b, long long m) { n   if (b == 0) return 1; n   long long t = (pw(a, b / 2, m)) % m; n   t = (t * t) % m; n   if (b % 2 == 0) return t; n   return (a * t) % m; n } n long long gc(long long a, long long b) { n   if (b == 0) return a; n   return gc(b, a % b); n } n vector<string> splt(string x, char w) { n   vector<string> v; n   string s; n   int l = x.length(); n   for (long long i = 0; i < l; i++) { n     if (x[i] == w) { n       v.push_back(s); n       s =   ; n     } else { n       s.push_back(x[i]); n     } n   } n   v.push_back(s); n   return v; n } n class node { n  public: n   long long id, a, l; n   vector<node*> v; n   vector<pair<node*, long long>> vm; n   node* prt = NULL; n   long long s = -1, x = 0, y = 0; n   node(long long x = 0, long long y = 0) { n     id = y, a = x, l = 0; n     vm.clear(), v.clear(); n     prt = NULL; n     s = -1, x = 0, y = 0; n   } n   void adn(node* t) { v.push_back(t), l++; } n   void adn(node* t, long long x) { vm.push_back({t, x}), l++; } n }; n node* nnd(long long a = 0, long long i = 1) { n   node* t = new node(a, i); n   return t; n } n void solve() { n   long long n; n   cin >> n; n   char a[n][n]; n   for (long long i = 0; i < n; i++) n     for (long long j = 0; j < n; j++) cin >> a[i][j]; n   long long t = 0; n   for (long long i = 0; i < n; i++) n     for (long long j = 0; j < n; j++) { n       long long x = 0; n       if (i > 0 && a[i - 1][j] ==  o ) x++; n       if (j > 0 && a[i][j - 1] ==  o ) x++; n       if ((i < n - 1) && a[i + 1][j] ==  o ) x++; n       if ((j < n - 1) && a[i][j + 1] ==  o ) x++; n       if (x % 2 == 1) t++; n     } n   if (t == 0) n     cout <<  YES n ; n   else n     cout <<  NO n ; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   long long t = 1; n   long long f = t; n   while (f--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n void optimise() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n } n long long int mod = 998244353; n void normalize(long long int &a) { a = (a + mod) % mod; } n long long int modmul(long long int a, long long int b) { n   a = a % mod; n   b = b % mod; n   normalize(a); n   normalize(b); n   return (a * b) % mod; n } n long long int modadd(long long int a, long long int b) { n   a = a % mod; n   b = b % mod; n   normalize(a); n   normalize(b); n   return (a + b) % mod; n } n long long int modsub(long long int a, long long int b) { n   a = a % mod; n   b = b % mod; n   normalize(a); n   normalize(b); n   return (a - b + mod) % mod; n } n long long int me(long long int x, long long int n) { n   x %= mod; n   if (n == 0) return 1; n   long long int u = me(x, n / 2) % mod; n   u = (u * u) % mod; n   if (n % 2) u = (u * x) % mod; n   return u; n } n long long int me1(long long int x, long long int n) { n   if (n == 0) return 1; n   long long int u = me1(x, n / 2); n   u = u * u; n   if (n % 2) u = u * x; n   return u; n } n inline long long int modInv(long long int a) { return me(a, mod - 2); } n inline long long int modDiv(long long int a, long long int b) { n   return modmul(a, modInv(b)); n } n float power(float x, long long int y) { n   float temp; n   if (y == 0) return 1; n   temp = power(x, y / 2); n   if (y % 2 == 0) n     return temp * temp; n   else { n     if (y > 0) n       return x * temp * temp; n     else n       return (temp * temp) / x; n   } n } n long long int __gcd(long long int a, long long int b) { n   if (b == 0) n     return a; n   else n     return __gcd(b, a % b); n } n pair<long long int, long long int> operator+( n     pair<long long int, long long int> a, n     pair<long long int, long long int> b) { n   return {a.first + b.first, a.second + b.second}; n } n pair<long long int, long long int> operator-( n     pair<long long int, long long int> a, n     pair<long long int, long long int> b) { n   return {a.first - b.first, a.second - b.second}; n } n pair<long long int, long long int> operator*( n     pair<long long int, long long int> a, n     pair<long long int, long long int> b) { n   return {a.first * b.first, a.second * b.second}; n } n template <typename T> n ostream &operator<<(ostream &os, const set<T> &v) { n   for (auto it : v) os << it <<    ; n   return os; n } n template <typename T> n ostream &operator<<(ostream &os, const vector<T> &v) { n   for (long long int i = 0; i < v.size(); ++i) os << v[i] <<    ; n   return os; n } n void solve() { n   long long int n, h, reg; n   cin >> n >> h >> reg; n   set<pair<long long int, long long int>> s; n   vector<pair<long long int, pair<long long int, long long int>>> v; n   long long int x1 = 0; n   for (long long int i = 0; i < n; i++) { n     long long int x, y; n     cin >> x >> y; n     v.push_back({x, {y, i + 1}}); n     if (x == 100) x1 += y; n   } n   sort(v.begin(), v.end()); n   auto k1 = v.end(); n   k1--; n   if (reg >= x1) { n     cout <<  NO ; n     return; n   } n   long long int cur = h; n   long long int t = 0; n   set<long long int> used; n   vector<pair<long long int, long long int>> ans; n   long long int k = 0; n   cout <<  YES  n        <<   n ; n   while (cur > 0) { n     for (long long int i = 0; i < v.size(); i++) { n       if (used.find(v[i].second.second) == used.end()) { n         long long int p = v[i].first; n         if (p * h >= 100 * cur) s.insert(v[i].second); n       } n     } n     if (s.size() > 0) { n       auto k1 = s.end(); n       k1--; n       ans.push_back({t, k1->second}); n       k += (k1->first); n       t++; n       cur -= k; n       cur += reg; n       if (cur > h) cur = h; n       used.insert(k1->second); n       s.erase(k1); n     } else { n       t++; n       cur -= k; n       cur += reg; n       if (cur > h) cur = h; n     } n   } n   cout << t <<     << ans.size() <<   n ; n   for (long long int i = 0; i < ans.size(); i++) n     cout << ans[i].first <<     << ans[i].second <<   n ; n } n signed main() { n   optimise(); n   long long int t; n   t = 1; n   cout << fixed << setprecision(3); n   while (t--) { n     solve(); n     cout <<   n ; n   } n   cerr <<   n  n        <<  Time Elasped :   << 1.0 * clock() / CLOCKS_PER_SEC <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = 3.14159265359; n double ans, x, y; n int n, zeros, ones; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < 2 * n; ++i) { n     scanf( %lf , &y); n     x -= y - int(y); n     if (y == int(y)) n       ++zeros; n     else n       ++ones; n   } n   assert(zeros + ones == 2 * n); n   int i = 0; n   while (i < min(n, ones) && fabs(x) >= fabs(x + 1)) x += 1.0, ++i; n   while (i < n && zeros) ++i, --zeros; n   while (i < n) x += 1.0, ++i; n   printf( %.3lf , fabs(x)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[1000005]; n long long dp[1000005]; n multiset<int> se; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int i, c, j, k, l, m, n, t, r, cnt; n   int flag = 0; n   long long ans = 0, an = 0; n   t = 1; n   while (t--) { n     scanf( %d , &n); n     scanf( %d , &c); n     for (i = 1; i <= n; i++) { n       scanf( %d , &a[i]); n     } n     se.insert(0); n     for (i = 1; i < c; i++) { n       se.insert(a[i]); n       dp[i] = dp[i - 1] + a[i]; n     } n     an = dp[c - 1]; n     for (i = c; i <= n; i++) { n       se.insert(a[i]); n       se.erase(se.find(a[i - c])); n       an += (a[i] - a[i - c]); n       dp[i] = min(dp[i - 1] + a[i], dp[i - c] + an - *se.begin()); n     } n     cout << dp[n] << endl; n   } n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:102400000,102400000 ) n using namespace std; n template <class _T> n inline _T loww(_T n) { n   return n & (-n); n } n template <class _T> n inline void swapp(_T &x, _T &y) { n   _T t; n   t = x, x = y, y = t; n } n template <class _T> n inline _T minn(_T i, _T j) { n   return i > j ? j : i; n } n template <class _T> n inline _T maxx(_T i, _T j) { n   return i > j ? i : j; n } n template <class _T> n inline _T gcd(_T b, _T c) { n   return b && c ? (c ? gcd(c, b % c) : b) : b | c; n } n template <class _T> n inline _T abss(_T b) { n   return b > 0 ? b : -b; n } n struct node { n   int v, col; n }; n node a[500005 << 2]; n void DOWN(int l, int r, int dq) { n   if (a[dq].col) { n     if (l != r) { n       a[dq << 1].col = a[dq << 1].v = a[dq << 1 | 1].col = a[dq << 1 | 1].v = n           a[dq].col; n     } n     a[dq].col = 0; n   } n } n void UP(int l, int r, int dq) { n   DOWN(l, r, dq); n   if (a[dq << 1].v == a[dq << 1 | 1].v) n     a[dq].v = a[dq << 1].v; n   else n     a[dq].v = -1; n } n void U(int l, int r, int dq, int b, int c, int v) { n   DOWN(l, r, dq); n   if (l == r) { n     a[dq].v = v; n   } else { n     if (b <= l && r <= c) { n       a[dq].v = a[dq].col = v; n     } else { n       int mid = (l + r) >> 1; n       if (b <= mid) U(l, mid, dq << 1, b, c, v); n       if (c > mid) U(mid + 1, r, dq << 1 | 1, b, c, v); n       UP(l, r, dq); n     } n   } n } n int Q(int l, int r, int dq, int b) { n   DOWN(l, r, dq); n   if (l == r || a[dq].v != -1) n     return a[dq].v; n   else { n     int mid = (l + r) >> 1; n     if (b <= mid) n       return Q(l, mid, dq << 1, b); n     else n       return Q(mid + 1, r, dq << 1 | 1, b); n   } n } n void build(int l, int r, int dq) { n   if (l == r) { n     a[dq].col = a[dq].v = 0; n   } else { n     a[dq].col = a[dq].v = 0; n     int mid = (l + r) >> 1; n     build(l, mid, dq << 1); n     build(mid + 1, r, dq << 1 | 1); n   } n } n bool outflag = true; n void out(int b) { n   if (outflag) n     outflag = false; n   else n     printf(   ); n   printf( %d , b); n } n pair<int, int> goal[500005]; n int val[500005]; n map<int, int> mymap; n int pre[500005], gl = 0; n int en = 0; n int main() { n   int n, i, t, js, told, b; n   scanf( %d , &n); n   for (i = 1; i <= n; i++) { n     scanf( %d , &val[i]); n   } n   build(1, n, 1); n   for (i = 1; i <= n; i++) { n     pre[i] = mymap[val[i]]; n     mymap[val[i]] = i; n     t = pre[i]; n     told = i; n     js = 0; n     while (t > en) { n       if (js++ > 1) break; n       if (t + 1 <= told - 1) U(1, n, 1, t + 1, told - 1, val[i]); n       b = Q(1, n, 1, t); n       if (b != 0) { n         goal[gl++] = make_pair(b, val[i]); n         en = i; n         break; n       } n       told = t; n       t = pre[t]; n     } n     if (js == 3) { n       goal[gl++] = make_pair(val[i], val[i]); n       en = i; n     } n   } n   printf( %d n , gl * 4); n   for (i = 0; i < gl; i++) { n     out(goal[i].first); n     out(goal[i].second); n     out(goal[i].first); n     out(goal[i].second); n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[200010]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   for (int i = 1; i <= (n + 1) / 2; i++) { n     if (i % 2 == 1) { n       int temp; n       temp = a[i]; n       a[i] = a[n + 1 - i]; n       a[n + 1 - i] = temp; n     } n   } n   for (int i = 1; i < n; i++) printf( %d  , a[i]); n   printf( %d n , a[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 inch = n / 3; n   if (n % 3 == 2) inch++; n   cout << inch / 12 <<     << inch % 12; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 303; n int n; n bool eq[N][N]; n int dp[N][N]; n string s[N]; n int main() { n   cin >> n; n   int allsum = n - 1; n   for (int i = 0; i < n; ++i) { n     cin >> s[i]; n     allsum += s[i].size(); n   } n   for (int i = 0; i < n; ++i) { n     eq[i][i] = true; n     for (int j = 0; j < i; ++j) eq[i][j] = eq[j][i] = s[i] == s[j]; n   } n   for (int i = n - 1; i >= 0; --i) { n     for (int j = n - 1; j >= 0; --j) { n       if (eq[i][j]) { n         if (i + 1 < n && j + 1 < n) n           dp[i][j] = dp[i + 1][j + 1] + 1; n         else n           dp[i][j] = 1; n       } n     } n   } n   int ans = allsum; n   for (int i = 0; i < n; ++i) { n     int sum = 0; n     for (int j = 0; i + j < n; ++j) { n       sum += s[i + j].size(); n       int cnt = 1; n       for (int pos = i + j + 1; pos < n; ++pos) { n         if (dp[i][pos] > j) { n           ++cnt; n           pos += j; n         } n       } n       int cur = allsum - sum * cnt + (j + 1) * cnt - j * cnt; n       if (cnt > 1 && ans > cur) { n         ans = cur; n       } n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int par[21], siz[21]; n int root(int a) { n   while (a != par[a]) { n     a = par[a]; n   } n   return a; n } n void unionn(int a, int b) { n   int root_a = root(a); n   int root_b = root(b); n   if (root_a == root_b) { n     return; n   } n   if (siz[root_a] < siz[root_b]) { n     siz[root_b] += siz[root_a]; n     par[root_a] = root_b; n   } else { n     siz[root_a] += siz[root_b]; n     par[root_b] = root_a; n   } n } n void init() { n   int i; n   for (i = 0; i <= 20; i++) { n     siz[i] = 1; n     par[i] = i; n   } n } n int main() { n   int t; n   cin >> t; n   while (t--) { n     init(); n     int n; n     cin >> n; n     int i; n     string s1, s2; n     cin >> s1 >> s2; n     s1 =  @  + s1; n     s2 =  @  + s2; n     int flag = 0; n     for (i = 1; i <= n; i++) { n       if (s1[i] > s2[i]) { n         flag = 1; n       } n     } n     if (flag == 1) { n       cout <<  -1 n ; n       continue; n     } n     set<int> v[20]; n     int ans = 0; n     for (i = 1; i <= n; i++) { n       if (s1[i] != s2[i]) v[s2[i] -  a ].insert(s1[i] -  a ); n     } n     map<pair<int, int>, int> m; n     set<int> sp; n     for (i = 1; i <= 19; i++) { n       if (v[i].size() == 0) { n         continue; n       } n       set<int> sp; n       for (auto it = v[i].begin(); it != v[i].end(); it++) { n         sp.insert(root(*it)); n         unionn(*it, i); n       } n       ans += (int)(sp.size()); n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int w[100005]; n long long p[100005]; n int e[2005]; n int edge(int n) { n   int ans = n * (n - 1) / 2; n   if (!(n & 1)) ans += n / 2 - 1; n   return ans; n } n int main() { n   int n, m, q; n   cin >> n >> m; n   for (int i = 0; i < m; i++) cin >> q >> w[i]; n   sort(w, w + m, greater<int>()); n   m = min(m, 2005); n   while (m >= 2005 || edge(m) > n - 1) m--; n   long long ans = 0LL; n   for (int i = 0; i < m; i++) ans += w[i]; n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1000000007; n inline int add(int a, int b) { n   if ((a += b) >= mod) a -= mod; n   return a; n } n inline int mult(int a, int b) { n   long long t = 1ll * a * b; n   if (t >= mod) t %= mod; n   return t; n } n inline int dec(int a, int b) { n   if ((a -= b) < 0) a += mod; n   return a; n } n inline int power(int a, int b) { n   int out = 1; n   while (b) { n     if (b & 1) out = mult(out, a); n     a = mult(a, a); n     b >>= 1; n   } n   return out; n } n int n, a[2510], f[100010], ans, mx, sum; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]), sum += a[i]; n   mx = *max_element(a + 1, a + n + 1); n   f[1] = mult(mult(sum - 1, sum - 1), power(sum, mod - 2)); n   for (int i = 1; i < mx; i++) n     f[i + 1] = dec(add(f[i], f[i]), n                    add(f[i - 1], mult(sum - 1, power(sum - i, mod - 2)))); n   for (int i = 1; i <= n; i++) ans = add(ans, f[a[i]]); n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int solve(void) { n   int n, s; n   cin >> n >> s; n   set<int> avail; n   char c; n   for (int i = 0; i < n; i++) { n     cin >> c; n     avail.insert((int)c -  a  + 1); n   } n   int cnt = 1; n   int ans = *avail.begin(); n   int prev = ans; n   avail.erase(avail.begin()); n   while (cnt < s) { n     if (avail.empty()) return -1; n     while (*avail.begin() < prev + 2) { n       avail.erase(avail.begin()); n       if (avail.empty()) return -1; n     } n     prev = *avail.begin(); n     ans += prev; n     avail.erase(avail.begin()); n     cnt++; n   } n   return ans; n } n int main(void) { cout << solve() << endl; } n


#include <bits/stdc++.h> n using namespace std; n inline long long mod(long long n, long long m) { n   long long ret = n % m; n   if (ret < 0LL) ret += m; n   return ret; n } n long long gcd(long long a, long long b) { return (b == 0 ? a : gcd(b, a % b)); } n long long exp(long long a, long long b, long long m) { n   if (b == 0LL) return 1LL; n   if (b == 1LL) return mod(a, m); n   long long k = mod(exp(a, b / 2, m), m); n   if (b & 1LL) { n     return mod(a * mod(k * k, m), m); n   } else n     return mod(k * k, m); n } n const long long MAXN = 300100; n long long res[MAXN]; n pair<long long, long long> P[MAXN]; n long long dist(pair<long long, long long> a, pair<long long, long long> b) { n   return abs(a.first - b.first) + abs(a.second - b.second); n } n long long n; n long long dp[MAXN][3][4][4]; n const long long inf = 2e9; n long long solve(long long id, long long j, long long xo, long long yo) { n   if (j == 3) { n     if (xo == 3 && yo == 3) return 0; n     return -inf; n   } n   if (id == n + 1) { n     return -inf; n   } n   long long &x = dp[id][j][xo][yo]; n   if (x != -1) return x; n   x = 0; n   long long op0 = solve(id + 1, j, xo, yo); n   x = op0; n   for (long long it1 = 0; it1 <= 2; it1++) { n     for (long long it2 = 0; it2 <= 2; it2++) { n       long long sum1 = it1 * P[id].first - (2 - it1) * P[id].first; n       long long sum2 = it2 * P[id].second - (2 - it2) * P[id].second; n       if (it1 + xo <= 3 && it2 + yo <= 3) { n         x = max(x, sum1 + sum2 + solve(id + 1, j + 1, xo + it1, yo + it2)); n       } n     } n   } n   return x; n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   cin >> n; n   for (long long i = 1; i <= n; i++) { n     cin >> P[i].first >> P[i].second; n   } n   memset(dp, -1, sizeof(dp)); n   long long tot = 0; n   for (long long i = 1; i <= n; i++) { n     tot += dist(P[i], P[(i == n ? 1 : i + 1)]); n   } n   cout << solve(1, 0, 0, 0) <<    ; n   for (long long i = 4; i <= n; i++) cout << tot <<    ; n   cout <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int s[100100]; n int main() { n   scanf( %d , &n); n   if (n % 4 == 2 || n % 4 == 3) n     puts( -1 ); n   else { n     for (int i = 0; i < n / 2; i += 2) n       s[i] = i + 2, s[i + 1] = n - i, s[n - i - 1] = n - i - 1, n       s[n - i - 2] = i + 1; n     if (n % 4 == 1) s[n / 2] = n / 2 + 1; n     for (int i = 0; i < n; i++) printf( %d  , s[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[200000]; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n   } n   sort(a, a + n); n   int q; n   cin >> q; n   for (int i = 0; i < q; ++i) { n     int m; n     cin >> m; n     int l = 0, r = n - 1; n     while (l <= r) { n       int mid = (l + r) / 2; n       if (a[mid] > m) { n         r = mid - 1; n       } else { n         l = mid + 1; n       } n     } n     cout << l << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1010; n int n, m; n int g[N][N]; n int ans = 0; n inline void Solve(int l, int r, int K, int dep) { n   if (l >= r) return; n   ans = ((ans) < (dep) ? (dep) : (ans)); n   int siz = (r - l + 1) / K; n   if (!siz) ++siz; n   int last = l, cnt = 0; n   vector<pair<int, int> > S; n   while (last <= r && cnt < K) n     ++cnt, Solve(last, cnt == K ? r : min(last + siz - 1, r), K, dep + 1), n         S.push_back(make_pair(last, cnt == K ? r : min(last + siz - 1, r))), n         last += siz; n   cerr <<  Solve(  << l <<  ,  << r <<  ,  << K <<  ,  << dep <<  ) n ; n   cerr <<  S:  ; n   for (auto x : S) cerr <<  [  << x.first <<  ,  << x.second <<  ]  ; n   cerr << endl; n   for (register int i = 0; i < (int)S.size(); ++i) n     for (register int j = i + 1; j < (int)S.size(); ++j) n       for (register int k = S[i].first; k <= S[i].second; ++k) n         for (register int l = S[j].first; l <= S[j].second; ++l) g[k][l] = dep; n } n int main() { n   scanf( %d%d , &n, &m); n   for (register int len = 1; len < n; len *= m) { n     ++ans; n     for (register int i = 0; i < n; i += len) n       for (register int j = 0; j < n; j += len) n         if (i != j) n           for (register int x = 0; x < len && i + x < n; ++x) n             for (register int y = 0; y < len && j + y < n; ++y) n               g[i + x + 1][j + y + 1] = ans; n   } n   printf( %d n , ans); n   for (register int i = 1; i <= n; ++i) n     for (register int j = i + 1; j <= n; ++j) printf( %d  , g[i][j]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, l, sum = 0; n   cin >> n; n   string st; n   for (l = 0; l < n; l++) { n     cin >> st; n     if (st[0] ==  T ) n       sum = sum + 4; n     else if (st[0] ==  C ) n       sum = sum + 6; n     else if (st[0] ==  O ) n       sum = sum + 8; n     else if (st[0] ==  D ) n       sum = sum + 12; n     else n       sum = sum + 20; n   } n   cout << sum; n   return 0; n } n


#include <bits/stdc++.h> n const long long inf = 1000000000; n const long long inf64 = inf * inf; n const double pi = acos(-1.0); n long long Abs(long long x) { return (x >= 0 ? x : -x); } n using namespace std; n bool solve() { n   int n; n   cin >> n; n   vector<vector<long long> > mat(n, vector<long long>(n)); n   vector<long long> ans(n, 0); n   for (int i(0); i < n; i++) n     for (int j(0); j < n; j++) cin >> mat[i][j]; n   for (int i(0); i < n; i++) { n     for (int j(0); j < n; j++) { n       if (i == j) continue; n       ans[j] |= mat[i][j]; n     } n   } n   for (int i(0); i < n; i++) cout << ans[i] <<    ; n   return true; n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { n   if (b > a) { n     return gcd(b, a); n   } n   if (b == 0) { n     return a; n   } n   return gcd(b, a % b); n } n long long expo(long long a, long long b, long long mod) { n   long long res = 1; n   while (b > 0) { n     if (b & 1) res = (res * a) % mod; n     a = (a * a) % mod; n     b = b >> 1; n   } n   return res; n } n vector<int> sieve(int n) { n   int *arr = new int[n + 1](); n   vector<int> vect; n   for (int i = 2; i <= n; i++) n     if (arr[i] == 0) { n       vect.push_back(i); n       for (int j = 2 * i; j <= n; j += i) arr[j] = 1; n     } n   return vect; n } n void extendgcd(long long a, long long b, long long *v) { n   if (b == 0) { n     v[0] = 1; n     v[1] = 0; n     v[2] = a; n     return; n   } n   extendgcd(b, a % b, v); n   long long x = v[1]; n   v[1] = v[0] - v[1] * (a / b); n   v[0] = x; n   return; n } n long long mminv(long long a, long long b) { n   long long arr[3]; n   extendgcd(a, b, arr); n   return arr[0]; n } n long long mminvprime(long long a, long long b) { return expo(a, b - 2, b); } n bool revsort(long long a, long long b) { return a > b; } n void swap(int &x, int &y) { n   int temp = x; n   x = y; n   y = temp; n } n long long combination(long long n, long long r, long long m, long long *fact) { n   long long val1 = fact[n]; n   long long val2 = mminvprime(fact[r], m); n   long long val3 = mminvprime(fact[n - r], m); n   return ((val1 * val2) % m * val3) % m; n } n void dfs(int start, vector<int> *edges, bool *visited, int *blues, int *reds, n          int *color, vector<pair<int, int>> &childs) { n   if (color[start] == 1) blues[start]++; n   if (color[start] == 2) reds[start]++; n   visited[start] = true; n   for (int i = 0; i < edges[start].size(); i++) { n     if (visited[edges[start][i]]) continue; n     dfs(edges[start][i], edges, visited, blues, reds, color, childs); n     blues[start] += blues[edges[start][i]]; n     reds[start] += reds[edges[start][i]]; n     childs.push_back({start, edges[start][i]}); n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int n; n   cin >> n; n   int color[n]; n   int sum1 = 0; n   int sum2 = 0; n   for (int i = 0; i < n; i++) { n     cin >> color[i]; n     if (color[i] == 1) sum1++; n     if (color[i] == 2) sum2++; n   } n   vector<int> *edges = new vector<int>[n]; n   vector<int> *ans1 = new vector<int>[n]; n   for (int i = 0; i < n - 1; i++) { n     int u, v; n     cin >> u >> v; n     u--; n     v--; n     edges[u].push_back(v); n     edges[v].push_back(u); n     ans1[u].push_back(1); n     ans1[v].push_back(1); n   } n   int *blues = new int[n](); n   int *reds = new int[n](); n   bool *visited = new bool[n](); n   vector<pair<int, int>> childs; n   dfs(0, edges, visited, blues, reds, color, childs); n   int ans = 0; n   set<pair<int, int>> s; n   for (int i = 0; i < childs.size(); i++) { n     int x = childs[i].second; n     int blue = blues[x]; n     int red = reds[x]; n     if (blue == 0 && red == sum2) ans++; n     if (red == 0 && blue == sum1) ans++; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int N; n   string team1 =   ; n   string team2 =   ; n   int team1points = 0; n   int team2points = 0; n   cin >> N; n   while (N--) { n     string goalTeam; n     cin >> goalTeam; n     if (team1 == goalTeam) { n       team1points++; n     } else if (team2 == goalTeam) { n       team2points++; n     } else if (team1 ==   ) { n       team1 = goalTeam; n       team1points++; n     } else if (team2 ==   ) { n       team2 = goalTeam; n       team2points++; n     } n   } n   cout << (team1points > team2points ? team1 : team2) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool possible = 1; n void change(char str[], char mask[]) { n   char best[6]; n   best[0] =   0 ; n   for (int i = 0; i < (4); ++i) { n     for (int d = 0; d < (10); ++d) n       if (!(i == 0 && d == 0)) { n         char temp = str[i]; n         str[i] = (d +  0 ); n         if (strcmp(str, mask) >= 0) { n           if (best[0] ==   0 ) n             strcpy(best, str); n           else if (strcmp(best, str) > 0) n             strcpy(best, str); n         } n         str[i] = temp; n       } n   } n   if (strcmp(best,  2011 ) > 0) possible = 0; n   strcpy(str, best); n } n int main() { n   int n; n   scanf( %d , &n); n   char in[1005][6]; n   for (int i = 0; i < (n); ++i) scanf( %s , in[i]); n   change(in[0],  1000 ); n   possible = 1; n   for (int i = (1); i <= (n - 1); ++i) { n     change(in[i], in[i - 1]); n   } n   if (!possible) n     puts( No solution ); n   else { n     for (int i = 0; i < (n); ++i) printf( %s n , in[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   cin.tie(0); n   cin.sync_with_stdio(0); n   int n; n   cin >> n; n   vector<vector<int> > p(n); n   for (int i = 1; i < n; i++) { n     int tmp; n     cin >> tmp; n     p[tmp - 1].push_back(i); n   } n   int ans = 0; n   queue<pair<int, int> > q; n   q.push(pair<int, int>(0, 0)); n   vector<int> tmp(n, 0); n   while (q.size()) { n     pair<int, int> front = q.front(); n     int cur = front.first; n     int level = front.second; n     q.pop(); n     tmp[level] = !tmp[level]; n     for (auto it : p[cur]) q.push(pair<int, int>(it, level + 1)); n   } n   for (int i = 0; i < n; i++) ans += tmp[i]; n   cout << ans << endl; n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n int a, b; n bool check(int cnt) { n  int cnt6 = cnt/6; n  int cnt2 = cnt/2 - cnt6; n  int cnt3 = cnt/3 - cnt6; n  if(cnt2 <= a) { n   cnt6 -= a-cnt2; n  } n  if(cnt3 <= b) { n   cnt6 -= b-cnt3; n  } n  return cnt6 >= 0; n } n int main() { n #ifdef DEBUG n     a = 0, b = 1000000; n #elif not defined(DEBUG) n  cin>>a>>b; n #endif n  int l = max(a<<1, b*3); n  int r = l<<1; n  while(l+100<r) { n   int mid = (l+r)>>1; n   if(check(mid)) r = mid; else l = mid; n  } n  for(int i = l;i<=r;i++) if(check(i)) { n   cout<<i<<endl; n   break; n  } n  return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e6 + 5; n const long long MOD = 1e9 + 7; n long long n, ct; n long long a[N], vis[N]; n vector<long long> g[N]; n void dfs(long long k) { n   if (vis[k]) return; n   vis[k] = 1; n   ct++; n   for (auto it : g[k]) dfs(it); n } n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   cin >> n; n   for (long long i = 1; i <= n; i++) { n     cin >> a[i]; n     g[i].push_back(a[i]); n   } n   vector<long long> ans; n   for (long long i = 1; i <= n; i++) { n     if (!vis[i]) { n       ct = 0; n       dfs(i); n       ans.push_back(ct); n     } n   } n   sort(ans.rbegin(), ans.rend()); n   long long answer = 0; n   long long sz = ans.size(); n   for (long long i = 0; i < sz; i++) { n     answer += ans[i] * ans[i]; n   } n   if (sz >= 2) { n     answer -= ans[0] * ans[0]; n     answer -= ans[1] * ans[1]; n     answer += (ans[0] + ans[1]) * (ans[0] + ans[1]); n   } n   cout << answer; n } n


#include <bits/stdc++.h> n using namespace std; n int mx, cnt, k; n char s[105]; n int main() { n   while (~scanf(  %s , &s)) { n     k = strlen(s); n     mx = 0; n     for (int i = 0; i < k; i++) { n       for (int j = i + 1; j < k; j++) { n         cnt = 0; n         if (s[i] == s[j]) { n           for (int o = 0; o < k; o++) { n             if (s[i + o] == s[j + o]) n               cnt++; n             else n               break; n           } n         } n         if (cnt > mx) mx = cnt; n       } n     } n     printf( %d n , mx); n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int vis[2005]; n int main() { n   int n, m, k; n   char c; n   scanf( %d%d%d , &n, &m, &k); n   int ans = 0; n   for (int i = 0; i < n; i++) { n     getchar(); n     int temp = 0; n     for (int j = 0; j < m; j++) { n       scanf( %c , &c); n       if (c ==  . ) { n         vis[j]++; n         temp++; n       } else { n         vis[j] = 0; n         temp = 0; n       } n       if (temp >= k) ans++; n       if (vis[j] >= k && k != 1) ans++; n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxN = 300, maxTreeSize = 1 << 10; n struct Tree { n  private: n   bool active[maxTreeSize]; n   int Min[maxTreeSize], Max[maxTreeSize], offset; n  n  public: n   void resize(int n) { n     offset = 1; n     while (offset < n) offset *= 2; n   } n   void add(int v, int val) { n     v += offset; n     active[v] = true; n     Min[v] = Max[v] = val; n     while (v > 1) { n       v /= 2; n       active[v] = true; n       if (active[v * 2] and active[v * 2 + 1]) { n         Min[v] = min(Min[v * 2], Min[v * 2 + 1]); n         Max[v] = max(Max[v * 2], Max[v * 2 + 1]); n       } else if (active[v * 2]) { n         Min[v] = Min[v * 2]; n         Max[v] = Max[v * 2]; n       } else { n         Min[v] = Min[v * 2 + 1]; n         Max[v] = Max[v * 2 + 1]; n       } n     } n   } n   pair<int, int> query(int val) { n     int v = 1; n     while (v < offset) { n       if (!active[v * 2] or !active[v * 2 + 1]) { n         v = active[v * 2] ? v * 2 : v * 2 + 1; n         continue; n       } n       if (val <= Max[v * 2]) { n         v = v * 2; n         continue; n       } n       if (Min[v * 2 + 1] <= val) { n         v = v * 2 + 1; n         continue; n       } n       int diff1 = abs(Max[v * 2] - val), diff2 = abs(Min[v * 2 + 1] - val); n       v = (diff1 <= diff2) ? v * 2 : v * 2 + 1; n     } n     return make_pair(v - offset, abs(val - Max[v])); n   } n   void clear() { n     for (int i = 1; i < offset * 2; i++) active[i] = false; n   } n }; n int sumpUp[maxN + 1][maxN + 1], sumpDown[maxN + 1][maxN + 1], n     sumpLeft[maxN + 1][maxN + 1], sumpRight[maxN + 1][maxN + 1], n     sump1[maxN + 1], sump2[maxN + 1], T[maxN + 1][maxN + 1]; n int tp, tu, td, ord_in_tree[maxN + 1]; n pair<int, int> temp[maxN]; n int cost_of_step(int a, int b) { n   if (a == b) return tp; n   if (a < b) return tu; n   if (a > b) return td; n } n Tree tree; n int main() { n   int n, m, ts; n   scanf( %d%d%d%d%d%d , &n, &m, &ts, &tp, &tu, &td); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) scanf( %d , &T[i][j]); n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 2; j <= m; j++) n       sumpLeft[i][j] = sumpLeft[i][j - 1] + cost_of_step(T[i][j - 1], T[i][j]); n     for (int j = m - 1; j > 0; j--) n       sumpRight[i][j] = n           sumpRight[i][j + 1] + cost_of_step(T[i][j + 1], T[i][j]); n   } n   for (int j = 1; j <= m; j++) { n     for (int i = 2; i <= n; i++) n       sumpUp[i][j] = sumpUp[i - 1][j] + cost_of_step(T[i - 1][j], T[i][j]); n     for (int i = n - 1; i > 0; i--) n       sumpDown[i][j] = sumpDown[i + 1][j] + cost_of_step(T[i + 1][j], T[i][j]); n   } n   tree.resize(m); n   int min_diff = 1000000000; n   int i1, j1, i2, j2; n   for (int a = 1; a <= n; a++) { n     for (int b = a + 2; b <= n; b++) { n       for (int j = 1; j <= m; j++) { n         sump1[j] = sumpLeft[a][j] + (sumpUp[b][j] - sumpUp[a][j]) + n                    sumpRight[b][1] - sumpRight[b][j]; n         sump2[j] = sumpLeft[a][j] - (sumpDown[a][j] - sumpDown[b][j]) + n                    (sumpRight[b][1] - sumpRight[b][j]); n         temp[j - 1] = make_pair(sump2[j], j); n       } n       sort(temp, temp + m); n       for (int j = 0; j < m; j++) ord_in_tree[temp[j].second] = j; n       tree.add(ord_in_tree[1], sump2[1]); n       for (int j = 3; j <= m; j++) { n         pair<int, int> res_temp = tree.query(sump1[j] - ts); n         if (res_temp.second < min_diff) { n           min_diff = res_temp.second; n           i1 = a; n           j1 = temp[res_temp.first].second; n           i2 = b; n           j2 = j; n         } n         tree.add(ord_in_tree[j - 1], sump2[j - 1]); n       } n       tree.clear(); n     } n   } n   printf( %d %d %d %d n , i1, j1, i2, j2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool Caps(string a) { n   for (int i = 0; i < a.size(); i++) n     if (a.at(i) < 65 && a.at(i) > 90) return false; n   return true; n } n int main() { n   long long n, cnt = 0; n   cin >> n; n   while (n > 0) { n     int rem = n % 10; n     if (rem == 4 || rem == 7) { n       cnt++; n     } n     n /= 10; n   } n   if (cnt == 4 || cnt == 7) n     cout <<  YES ; n   else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = (int)1e9; n const int mod = inf + 7; n const double eps = 1e-9; n const double pi = acos(-1.0); n int n, cnt, m; n string second, t; n vector<string> g[100]; n map<string, int> id; n map<int, string> st; n bool check(string second, string t) { n   string cur; n   if ((int)second.size() > (int)t.size()) return 1; n   for (int i = (int)t.size() - (int)second.size(); i < (int)t.size(); ++i) { n     cur += t[i]; n   } n   if (cur == second) return 0; n   return 1; n } n void make(int pos) { n   vector<string> v; n   map<string, bool> x; n   for (int i = 0; i < (int)g[pos].size(); ++i) { n     if (x.count(g[pos][i]) == 0) { n       v.push_back(g[pos][i]); n       x[g[pos][i]] = 1; n     } n   } n   g[pos] = v; n   v.clear(); n   for (int i = 0; i < (int)g[pos].size(); ++i) { n     bool bad = 0; n     for (int j = 0; j < (int)g[pos].size(); ++j) { n       if (i == j) continue; n       if (!check(g[pos][i], g[pos][j])) { n         bad = 1; n       } n     } n     if (!bad) { n       v.push_back(g[pos][i]); n     } n   } n   g[pos] = v; n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0); n   cin >> n; n   for (int i = 0; i < n; ++i) { n     cin >> second; n     if (id.count(second) == 0) { n       id[second] = cnt; n       st[cnt] = second; n       cnt++; n     } n     int v = id[second]; n     cin >> m; n     for (int i = 0; i < m; ++i) { n       cin >> t; n       g[v].push_back(t); n     } n   } n   for (int i = 0; i < cnt; ++i) { n     make(i); n   } n   cout << cnt << endl; n   for (int i = 0; i < cnt; ++i) { n     cout << st[i] <<     << (int)g[i].size() <<    ; n     for (int j = 0; j < (int)g[i].size(); ++j) { n       cout << g[i][j] <<    ; n     } n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a, b; n   cin >> n >> a >> b; n   int ans = a; n   if (b < 0) { n     int t = abs(b); n     while (t--) { n       --a; n       if (a == 0) a = n; n     } n   } else { n     while (b--) { n       ++a; n       if (a == n + 1) a = 1; n     } n   } n   cout << a << endl; n } n


#include <bits/stdc++.h> n using namespace std; n map<char, int> U1; n map<char, int> U2; n map<char, int> U3; n int n, len; n int panduan(int a) { n   if (a != len) n     a = min(a + n, len); n   else if (a == len && n == 1) n     a = a - 1; n   return a; n } n int main() { n   cin >> n; n   string a, b, c; n   cin >> a; n   cin >> b; n   cin >> c; n   int Kuro = 0, Shiro = 0, Katie = 0; n   len = a.size(); n   for (int i = 0; i < len; i++) { n     U1[a[i]]++; n     if (U1[a[i]] > Kuro) Kuro = U1[a[i]]; n   } n   for (int i = 0; i < len; i++) { n     U2[b[i]]++; n     if (U2[b[i]] > Shiro) Shiro = U2[b[i]]; n   } n   for (int i = 0; i < len; i++) { n     U3[c[i]]++; n     if (U3[c[i]] > Katie) Katie = U3[c[i]]; n   } n   Kuro = panduan(Kuro); n   Shiro = panduan(Shiro); n   Katie = panduan(Katie); n   if (Kuro > Shiro && Kuro > Katie) n     printf( Kuro n ); n   else if (Shiro > Kuro && Shiro > Katie) n     printf( Shiro n ); n   else if (Katie > Kuro && Katie > Shiro) n     printf( Katie n ); n   else n     printf( Draw n ); n } n


#include <bits/stdc++.h> n using namespace std; n struct trip { n   int a, b, c; n }; n const int MAXINT = 100; n int n, temp; n vector<int> pos, neg; n bool zeros = false; n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> temp; n     if (temp > 0) n       pos.push_back(temp); n     else if (temp < 0) n       neg.push_back(temp); n     else n       zeros = true; n   } n   if (!pos.size() && !neg.size()) cout << 0 << endl; n   if (pos.size() == 0 && neg.size() == 1) { n     if (zeros) n       cout << 0 << endl; n     else n       cout << neg[0] << endl; n   } n   for (int e : pos) cout << e <<    ; n   if (neg.size() % 2 == 0) n     for (int e : neg) cout << e <<    ; n   else { n     sort(neg.begin(), neg.end()); n     int N = neg.size(); n     for (int i = 0; i < N - 1; i++) cout << neg[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<long long int, long long int> > v; n long long int indegree[300005]; n long long int vis[300005]; n long long int parent[300005]; n long long int getpar(long long int child) { n   if (parent[child] == child) n     return child; n   else n     return parent[child] = getpar(parent[child]); n } n void merge(long long int v1, long long int v2) { n   parent[getpar(v1)] = getpar(v2); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int TESTS = 1; n   while (TESTS--) { n     long long int n, m; n     cin >> n >> m; n     v.resize(m); n     memset(indegree, 0, sizeof(indegree)); n     memset(vis, 0, sizeof(vis)); n     for (long long int i = 1; i <= n; i++) { n       parent[i] = i; n     } n     for (long long int i = 0; i < m; i++) { n       cin >> v[i].first >> v[i].second; n       indegree[v[i].first]++; n       indegree[v[i].second]++; n     } n     long long int maxi = 0, idx = -1; n     for (long long int i = 1; i <= n; i++) { n       if (indegree[i] >= maxi) { n         maxi = indegree[i]; n         idx = i; n       } n     } n     for (long long int i = 0; i < m; i++) { n       if (v[i].first == idx || v[i].second == idx) { n         vis[i] = 1; n         merge(v[i].first, v[i].second); n       } n     } n     for (long long int i = 0; i < m; i++) { n       if (vis[i] == 0) { n         if (getpar(v[i].first) != getpar(v[i].second)) { n           merge(v[i].first, v[i].second); n           vis[i] = 1; n         } n       } n     } n     for (long long int i = 0; i < m; i++) { n       if (vis[i] == 1) { n         cout << v[i].first <<     << v[i].second <<   n ; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int k; n   cin >> k; n   string str, s; n   cin >> str; n   s = str; n   for (long long int i = 1; i < k; i++) str = str + s; n   map<char, vector<long long int>> pos; n   long long int i = 0; n   for (auto c : str) { n     pos[c].push_back(i); n     i++; n   } n   long long int n; n   cin >> n; n   char c; n   long long int p; n   while (n--) { n     cin >> p >> c; n     str[pos[c][p - 1]] = 0; n     pos[c].erase(pos[c].begin() + (p - 1)); n   } n   for (auto c : str) n     if (c) cout << c; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long M = 100100 * 5; n const long long N = 50005; n long long m, b, k; n map<long long, long long> fre; n int v1[M], mmax; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> m; n   for (int i = 0; i < m; i++) { n     cin >> b; n     fre[b]++; n   } n   for (int i = 0; i < N; i++) { n     if (fre[i]) v1[k] = i, k++, fre[i]--, mmax = max(mmax, i); n   } n   for (int i = mmax - 1; i >= 0; i--) { n     if (fre[i]) v1[k] = i, k++; n   } n   cout << k << endl; n   for (int i = 0; i < k; i++) cout << v1[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> arr, hgt, res; n int n; n int func(int i, int j) {} n int main() { n   int a, b, c, d, e, f, g; n   cin >> a; n   while (a--) { n     cin >> b; n     vector<int> num; n     for (int i = 0; i < b; i++) { n       cin >> c; n       num.push_back(c); n     } n     if (num[b - 1] < num[0]) { n       cout <<  NO  << endl; n     } else { n       cout <<  YES  << endl; n     } n     num.clear(); n   } n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:36777216 ) n using namespace std; n const int inf = 1 << 30; n const long long Inf = 1LL << 62; n const int dx[] = {-1, 0, 1, 0, -1, -1, 1, 1, 0}; n const int dy[] = {0, 1, 0, -1, -1, 1, 1, -1, 0}; n const double eps = 1e-9; n const double pi = 3.1415926535897932384626433832795; n template <class T> n inline bool eq(const T &a, const T &b) { n   return fabs(a - b) < eps; n } n template <class T> n inline bool deg2(const T &n) { n   return (n & (n - 1)) == 0; n } n template <class T> n inline void chmax(T &a, const T &b) { n   a = max(a, b); n } n inline int intRand(int l, int r) { n   return ((rand() << 16) | rand()) % (r - l + 1) + l; n } n inline long long llRand(long long l, long long r) { n   return (((1ll * rand()) << 48) | ((1ll * rand()) << 32) | (rand() << 16) | n           rand()) % n              (r - l + 1) + n          l; n } n bool is[100500], dd; n int s[1000], a[100500], n, m, i, ii, cnt, j, pr[100000], pp; n char q; n bool prime(int n) { n   for (int i = 2; i * i <= n; i++) n     if (n % i == 0) return 0; n   return 1; n } n int main() { n   ios_base::sync_with_stdio(false); n   srand(time(NULL)); n   for (i = 2; i < 100000; i++) n     if (prime(i)) pr[pp++] = i; n   scanf( %d%d n , &n, &m); n   while (m--) { n     scanf( %c%d n , &q, &i); n     if (q ==  + ) { n       if (is[i]) { n         puts( Already on ); n         continue; n       } n       ii = i; n       cnt = 0; n       for (j = 0; 1ll * pr[j] * pr[j] <= 1ll * i; j++) { n         dd = 0; n         while (i % pr[j] == 0) { n           i /= pr[j]; n           dd = 1; n         } n         if (dd) s[cnt++] = pr[j]; n       } n       if (i > 1) s[cnt++] = i; n       dd = 0; n       for (j = 0; j < cnt; j++) n         if (a[s[j]]) { n           printf( Conflict with %d n , a[s[j]]); n           dd = 1; n           break; n         } n       if (dd) continue; n       puts( Success ); n       for (j = 0; j < cnt; j++) a[s[j]] = ii; n       is[ii] = 1; n     } else { n       if (!is[i]) { n         printf( Already off n ); n         continue; n       } n       is[i] = 0; n       for (j = 0; 1ll * pr[j] * pr[j] <= 1ll * i; j++) { n         dd = 0; n         while (i % pr[j] == 0) { n           i /= pr[j]; n           a[pr[j]] = 0; n         } n       } n       if (i > 1) a[i] = 0; n       puts( Success ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e5 + 10, LOG = 23, MAX = 1e6 + 10, M = 1e9 + 7; n long long n; n long long mark[N], level[N], par[N][LOG + 10], arr[MAX], cnt[N], tavan[MAX]; n vector<long long> g[N]; n map<pair<long long, long long>, long long> mp; n void DFS(long long v) { n   mark[v] = 1; n   for (long long u : g[v]) { n     if (!mark[u]) { n       level[u] = level[v] + 1; n       par[u][0] = v; n       DFS(u); n     } n   } n } n long long DFS2(long long v) { n   mark[v] = 2; n   for (long long u : g[v]) { n     if (mark[u] != 2) cnt[v] += DFS2(u); n   } n   return cnt[v]; n } n long long getpar(long long v, long long dis) { n   long long cnt = 0; n   while (dis) { n     if (dis % 2) v = par[v][cnt]; n     cnt++; n     dis /= 2; n   } n   return v; n } n long long MOD(long long a) { return (((a % M) + M) % M); } n void UPDATE() { n   for (long long i = 1; i <= LOG; i++) { n     for (long long node = 1; node <= n; node++) n       par[node][i] = par[par[node][i - 1]][i - 1]; n   } n   tavan[0] = 1; n   for (long long i = 1; i < MAX; i++) { n     tavan[i] = MOD(tavan[i - 1] * 2); n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long u, v, x, q, ans = 0; n   cin >> n; n   for (long long i = 0; i < n - 1; i++) { n     cin >> u >> v >> x; n     mp[{u, v}] = x; n     g[u].push_back(v); n     g[v].push_back(u); n   } n   DFS(1); n   UPDATE(); n   cin >> q; n   arr[0] = 1; n   for (long long i = 1; i <= q; i++) { n     cin >> arr[i]; n     long long lca; n     v = arr[i]; n     u = arr[i - 1]; n     cnt[u]++; n     cnt[v]++; n     if (level[v] < level[u]) swap(v, u); n     v = getpar(v, level[v] - level[u]); n     if (v == u) n       lca = v; n     else { n       for (long long j = LOG; j >= 0; j--) { n         if (par[v][j] != par[u][j]) { n           v = par[v][j]; n           u = par[u][j]; n         } n       } n       lca = par[v][0]; n     } n     cnt[lca] -= 2; n   } n   DFS2(1); n   for (long long i = 1; i <= n; i++) { n     v = i; n     u = par[i][0]; n     if ((mp.count({v, u}) && mp[{v, u}]) || (mp.count({u, v}) && mp[{u, v}])) { n       if (cnt[v] % 2 == 0) n         ans += tavan[cnt[v] / 2] - 1; n       else { n         if (mp.count({v, u})) n           ans += tavan[(cnt[v] + 1) / 2] - 1; n         else n           ans += tavan[cnt[v] / 2] - 1; n       } n       ans = MOD(ans); n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline T MIN_3(T a, T b, T c) { n   return min(min(a, b), c); n } n template <class T> n inline T MAX_3(T a, T b, T c) { n   return max(max(a, b), c); n } n template <class T> n inline T BIGMOD(T n, T m, T mod) { n   long long int ans = 1; n   long long int k = n; n   while (m) { n     if (m & 1) { n       ans *= k; n       if (ans > mod) ans %= mod; n     } n     k *= k; n     if (k > mod) k %= mod; n     m >>= 1; n   } n   return ans; n } n template <class T> n string toString(T n) { n   ostringstream ost; n   ost << n; n   ost.flush(); n   return ost.str(); n } n template <class T> n string toBinary(T n) { n   string ret =   ; n   while (n) { n     if (n % 2 == 1) n       ret +=  1 ; n     else n       ret +=  0 ; n     n >>= 1; n   } n   reverse(ret.begin(), ret.end()); n   return ret; n } n void combination(int n, vector<vector<int> > &ret) { n   ret.resize(n + 1, vector<int>(n + 1, 0)); n   for (int i = 1; i <= n; i++) { n     ret[i][0] = ret[i][i] = 1; n     for (int j = 1; j < i; j++) { n       ret[i][j] = ret[i - 1][j] + ret[i - 1][j - 1]; n     } n   } n } n int toInt(string s) { n   int r = 0; n   istringstream sin(s); n   sin >> r; n   return r; n } n long long int toLInt(string s) { n   long long int r = 0; n   istringstream sin(s); n   sin >> r; n   return r; n } n double toDouble(string s) { n   double r = 0; n   istringstream sin(s); n   sin >> r; n   return r; n } n vector<string> parse(string temp) { n   vector<string> ans; n   ans.clear(); n   string s; n   istringstream iss(temp); n   while (iss >> s) ans.push_back(s); n   return ans; n } n template <class 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 <class 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 template <class T> n inline T power(T b, T p) { n   if (p < 0) return -1; n   if (b <= 0) return -2; n   if (!p) return 1; n   return b * power(b, p - 1); n } n template <class T> n inline int asd(T &ret) { n   char r; n   bool start = false, neg = false; n   ret = 0; n   bool isaNumber = false; n   while (true) { n     r = getchar(); n     if (r == EOF) { n       return 0; n     } n     if ((r -  0  < 0 || r -  0  > 9) && r !=  -  && !start) { n       continue; n     } n     if ((r -  0  < 0 || r -  0  > 9) && r !=  -  && start) { n       break; n     } n     if (start) ret *= 10; n     start = true; n     if (r ==  - ) n       neg = true; n     else n       ret += r -  0 ; n   } n   if (neg) ret *= -1; n   return 1; n } n template <class T> n inline void asdasd(T x, char y) { n   if (x < 0) { n     putchar( - ); n     x = -x; n   } n   char buf[21], *p = buf; n   do { n     *p++ =  0  + x % 10; n     x /= 10; n   } while (x); n   do { n     putchar(*--p); n   } while (p > buf); n   if (y == 0) n     putchar(  n ); n   else if (y == 1) n     putchar(   ); n } n vector<int> pr; n int inp[101]; n int dp[101][1 << 16]; n int go[101][1 << 16]; n int n; n int bp[61]; n void make(int &ret, int n) { n   for (int i = 0; i <= pr.size() - 1; i++) { n     if (n % pr[i] == 0) ret |= (1 << i); n   } n } n int inf = 100000000, sat = (1 << 16) - 1; n int rec(int pos, int mask) { n   if (pos == n) return 0; n   int &ret = dp[pos][mask]; n   if (ret != -1) return ret; n   ret = inf; n   int now; n   for (int i = 1; i <= 58; i++) { n     if ((bp[i] & mask) == 0) { n       now = abs(inp[pos] - i) + rec(pos + 1, mask | bp[i]); n       if (ret > now) { n         ret = now; n         go[pos][mask] = i; n       } n     } n   } n   return ret; n } n vector<int> ans; n void path(int pos, int mask) { n   if (pos == n) return; n   ans.push_back(go[pos][mask]); n   path(pos + 1, mask | bp[go[pos][mask]]); n } n int main() { n   for (int i = 2; i <= 60; i++) { n     bool ok = true; n     for (int j = 2; j < i; j++) { n       if (i % j == 0) { n         ok = false; n         break; n       } n     } n     if (ok) pr.push_back(i); n   } n   for (int i = 2; i <= 60; i++) { n     make(bp[i], i); n   } n   asd(n); n   for (int i = 0; i <= n - 1; i++) { n     asd(inp[i]); n   } n   memset(dp, -1, sizeof(dp)); n   rec(0, 0); n   path(0, 0); n   for (int i = 0; i <= ans.size() - 1; i++) asdasd(ans[i], i != ans.size() - 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1e5 + 5; n int n; n char a[MAX]; n pair<char, int> b[MAX]; n int main() { n   cin >> (a + 1); n   n = strlen(a + 1); n   for (int i = 1; i <= n; ++i) { n     b[i].first = a[i]; n     b[i].second = i; n   } n   sort(b + 1, b + n + 1, n        [&](const pair<char, int> &a, const pair<char, int> &b) { n          if (a.first == b.first) { n            return a.second < b.second; n          } n          return a.first > b.first; n        }); n   for (int i = 1, j = 0; i <= n; ++i) { n     if (j < b[i].second) { n       cout << b[i].first; n       j = b[i].second; n     } n   } n   cout <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n struct Rect { n   int right, left, top, bottom; n   int height() { return top - bottom; } n   int width() { return right - left; } n   int area() { return (top - bottom) * (right - left); } n }; n void solve() { n   int w, h; n   cin >> w >> h; n   Rect first, second; n   second.bottom = 0, second.left = 0; n   cin >> first.left >> first.bottom >> first.right >> first.top; n   cin >> second.right >> second.top; n   bool move_y = first.height() + second.height() <= h, n        move_x = first.width() + second.width() <= w; n   if (!move_y && !move_x) { n     cout << -1 <<   n ; n     return; n   } else { n     vector<int> moves; n     if (move_x) { n       moves.push_back(second.right - first.left); n       moves.push_back(first.right - (w - second.width())); n     } n     if (move_y) { n       moves.push_back(second.top - first.bottom); n       moves.push_back(first.top - (h - second.height())); n     } n     int ans = INT_MAX; n     for (auto i : moves) { n       ans = min(ans, max(i, 0)); n     } n     cout << ans; n   } n   cout <<   n ; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int t; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 1e9 + 7; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   cin >> n; n   vector<long long> L(n), R(n), A(n); n   for (int i = 0; i < n; ++i) { n     cin >> L[i]; n   } n   for (int i = 0; i < n; ++i) { n     cin >> R[i]; n   } n   for (int i = 0; i < n; ++i) { n     A[i] = n - L[i] - R[i]; n   } n   for (int i = 0; i < n; ++i) { n     for (int j = i + 1; j < n; ++j) { n       if (A[j] > A[i]) { n         R[i]--; n       } n     } n     for (int j = 0; j < i; ++j) { n       if (A[j] > A[i]) { n         L[i]--; n       } n     } n   } n   for (int i = 0; i < n; ++i) { n     if (L[i] || R[i]) { n       cout <<  NO  n            <<   n ; n       return 0; n     } n   } n   cout <<  YES  n        <<   n ; n   for (int i : A) { n     cout << i <<    ; n   } n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T, typename U> n inline void smin(T &a, U b) { n   if (a > b) a = b; n } n template <typename T, typename U> n inline void smax(T &a, U b) { n   if (a < b) a = b; n } n template <typename T> n inline void gn(T &first) { n   char c, sg = 0; n   while (c = getchar(), (c >  9  || c <  0 ) && c !=  - ) n     ; n   for ((c ==  -  ? sg = 1, c = getchar() : 0), first = 0; c >=  0  && c <=  9 ; n        c = getchar()) n     first = (first << 1) + (first << 3) + c -  0 ; n   if (sg) first = -first; n } n template <class T, class T1> n inline void gn(T &first, T1 &second) { n   gn(first); n   gn(second); n } n template <class T, class T1, class T2> n inline void gn(T &first, T1 &second, T2 &z) { n   gn(first); n   gn(second); n   gn(z); n } n template <typename T> n inline void print(T first) { n   if (first < 0) { n     putchar( - ); n     return print(-first); n   } n   if (first < 10) { n     putchar( 0  + first); n     return; n   } n   print(first / 10); n   putchar(first % 10 +  0 ); n } n template <typename T> n inline void println(T first) { n   print(first), putchar(  n ); n } n template <typename T> n inline void printsp(T first) { n   print(first), putchar(   ); n } n template <class T, class T1> n inline void print(T first, T1 second) { n   printsp(first), println(second); n } n template <class T, class T1, class T2> n inline void print(T first, T1 second, T2 z) { n   printsp(first), printsp(second), println(z); n } n int power(int a, int b, int m, int ans = 1) { n   for (; b; b >>= 1, a = 1LL * a * a % m) n     if (b & 1) ans = 1LL * ans * a % m; n   return ans; n } n int main() { n   int a, b, p; n   long long first; n   gn(a, b, p); n   gn(first); n   long long ans = 0; n   long long M = (long long)p * (p - 1); n   int p1 = power(p - 1, p - 2, p); n   int p2 = p == 2 ? 1 : power(p, p - 3, p - 1); n   for (int r = 0; r < p - 1; r++) { n     int second = (long long)b * power(a, p - 1 - r, p) % p; n     long long rr = n         ((long long)r * p % M * p2 + (long long)second * (p - 1) % M * p1) % M; n     if (rr <= first) ans += (first - rr) / M + 1; n   } n   println(ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 505; n int n, m, qc; n bool fd[N][N]; n bitset<N> bs1[N][N]; n bitset<N> bs2[N][N]; n struct query { n   int x1, y1, x2, y2; n   int id; n }; n vector<query> qs; n vector<bool> ans; n void solve(vector<int>& ids, int tl, int tr) { n   if (ids.size() == 0) return; n   if (tl > tr) return; n   vector<int> l; n   vector<int> r; n   int tm = (tl + tr) / 2; n   for (int i = n - 1; i >= 0; i--) { n     bs1[i][tm].reset(); n     if (!fd[i][tm]) { n       bs1[i][tm][i] = true; n       if (i + 1 < n) bs1[i][tm] |= bs1[i + 1][tm]; n     } n   } n   for (int i = n - 1; i >= 0; i--) { n     for (int j = tm - 1; j >= tl; j--) { n       bs1[i][j].reset(); n       if (fd[i][j]) continue; n       if (i + 1 < n) bs1[i][j] |= bs1[i + 1][j]; n       bs1[i][j] |= bs1[i][j + 1]; n     } n   } n   for (int i = 0; i < n; i++) { n     bs2[i][tm].reset(); n     if (!fd[i][tm]) { n       bs2[i][tm][i] = true; n       if (i > 0) bs2[i][tm] |= bs2[i - 1][tm]; n     } n   } n   for (int i = 0; i < n; i++) { n     for (int j = tm + 1; j <= tr; j++) { n       bs2[i][j].reset(); n       if (fd[i][j]) continue; n       if (i - 1 >= 0) bs2[i][j] |= bs2[i - 1][j]; n       bs2[i][j] |= bs2[i][j - 1]; n     } n   } n   for (int id : ids) { n     query& q = qs[id]; n     if (q.y1 < tm && q.y2 < tm) { n       l.push_back(id); n     } else if (q.y1 > tm && q.y2 > tm) { n       r.push_back(id); n     } else { n       bitset<N> bans = bs1[q.x1][q.y1] & bs2[q.x2][q.y2]; n       ans[id] = bans.any(); n     } n   } n   solve(l, tl, tm - 1); n   solve(r, tm + 1, tr); n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   ; n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       char c; n       cin >> c; n       fd[i][j] = (c ==  # ); n     } n   } n   cin >> qc; n   qs.resize(qc); n   for (int i = 0; i < qc; i++) { n     int x1, y1, x2, y2; n     cin >> x1 >> y1 >> x2 >> y2; n     x1--; n     y1--; n     x2--; n     y2--; n     qs[i] = {x1, y1, x2, y2, i}; n   } n   vector<int> ids; n   ids.reserve(qc); n   for (int i = 0; i < qc; i++) { n     if (qs[i].x1 > qs[i].x2 || qs[i].y1 > qs[i].y2) continue; n     ids.push_back(i); n   } n   ans.resize(qc); n   solve(ids, 0, m - 1); n   for (bool t : ans) cout << (t ?  Yes  :  No ) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2015; n const int inf = 1 << 29; n int n, h, a[maxn], l[maxn], r[maxn]; n double p, dp[maxn][maxn][2][2]; n int main() { n   cin >> n >> h >> p; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   sort(a, a + n); n   for (int i = 0; i < n; i++) { n     int lb = i - 1; n     for (; lb >= 0 && a[lb + 1] - a[lb] < h; lb--) n       ; n     l[i] = lb; n     int rb = i + 1; n     for (; rb < n && a[rb] - a[rb - 1] < h; rb++) n       ; n     r[i] = rb; n   } n   for (int len = 1; len <= n; len++) { n     for (int leftFall = 0; leftFall < 2; leftFall++) { n       for (int rightFall = 0; rightFall < 2; rightFall++) { n         for (int start = 0; start + len - 1 < n; start++) { n           int end = start + len - 1; n           if (leftFall == 0) { n             int tmp = -inf; n             if (start > 0) { n               tmp = a[start - 1]; n             } n             dp[start][end][leftFall][rightFall] += n                 0.5 * p * n                 (min(h, a[start] - tmp) + dp[start + 1][end][0][rightFall]); n           } else { n             int tmp = -inf; n             if (start > 0) { n               tmp = a[start - 1] + h; n             } n             dp[start][end][leftFall][rightFall] += n                 0.5 * p * n                 (min(h, a[start] - tmp) + dp[start + 1][end][0][rightFall]); n           } n           if (rightFall == 1) { n             int tmp = inf; n             if (end < n - 1) { n               tmp = a[end + 1]; n             } n             dp[start][end][leftFall][rightFall] += n                 0.5 * (1 - p) * n                 (min(h, tmp - a[end]) + dp[start][end - 1][leftFall][1]); n           } else { n             int tmp = inf; n             if (end < n - 1) { n               tmp = a[end + 1] - h; n             } n             dp[start][end][leftFall][rightFall] += n                 0.5 * (1 - p) * n                 (min(h, tmp - a[end]) + dp[start][end - 1][leftFall][1]); n           } n           if (end < r[start]) { n             dp[start][end][leftFall][rightFall] += n                 0.5 * (1 - p) * (a[end] - a[start]); n             if (rightFall == 1) { n               int tmp = inf; n               if (end < n - 1) { n                 tmp = a[end + 1]; n               } n               dp[start][end][leftFall][rightFall] += n                   0.5 * (1 - p) * min(h, tmp - a[end]); n             } else { n               int tmp = inf; n               if (end < n - 1) { n                 tmp = a[end + 1] - h; n               } n               dp[start][end][leftFall][rightFall] += n                   0.5 * (1 - p) * min(h, tmp - a[end]); n             } n           } else { n             dp[start][end][leftFall][rightFall] += n                 0.5 * (1 - p) * n                 (a[r[start] - 1] - a[start] + h + n                  dp[r[start]][end][1][rightFall]); n           } n           if (l[end] < start) { n             dp[start][end][leftFall][rightFall] += n                 0.5 * p * (a[end] - a[start]); n             if (leftFall == 0) { n               int tmp = -inf; n               if (start > 0) { n                 tmp = a[start - 1]; n               } n               dp[start][end][leftFall][rightFall] += n                   0.5 * p * min(h, a[start] - tmp); n             } else { n               int tmp = -inf; n               if (start > 0) { n                 tmp = a[start - 1] + h; n               } n               dp[start][end][leftFall][rightFall] += n                   0.5 * p * min(h, a[start] - tmp); n             } n           } else { n             dp[start][end][leftFall][rightFall] += n                 0.5 * p * n                 (a[end] - a[l[end] + 1] + h + dp[start][l[end]][leftFall][0]); n           } n         } n       } n     } n   } n   cout << setprecision(9) << fixed << dp[0][n - 1][0][0] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int getcom(pair<int, int> a, pair<int, int> b) { n   vector<int> all; n   all.push_back(a.first); n   all.push_back(a.second); n   all.push_back(b.first); n   all.push_back(b.second); n   sort(all.begin(), all.end()); n   int com = -1; n   for (int k = 1; k < 4; k++) { n     if (all[k] == all[k - 1]) { n       com = all[k]; n     } n   } n   return com; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   vector<pair<int, int> > a; n   vector<pair<int, int> > b; n   for (int i = 0; i < n; i++) { n     int x, y; n     cin >> x >> y; n     if (x > y) { n       swap(x, y); n     } n     a.push_back(make_pair(x, y)); n   } n   for (int i = 0; i < m; i++) { n     int x, y; n     cin >> x >> y; n     if (x > y) { n       swap(x, y); n     } n     b.push_back(make_pair(x, y)); n   } n   vector<int> possible; n   bool theyknow = true; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (a[i] == b[j]) { n         continue; n       } n       int com = getcom(a[i], b[j]); n       if (com == -1) { n         continue; n       } n       if (com != -1) { n         possible.push_back(com); n       } n       for (int k = 0; k < m; k++) { n         if (a[i] == b[k]) { n           continue; n         } n         int com2 = getcom(a[i], b[k]); n         if (com2 != -1 && com2 != com) { n           theyknow = false; n         } n       } n       for (int k = 0; k < n; k++) { n         if (a[k] == b[j]) { n           continue; n         } n         int com2 = getcom(a[k], b[j]); n         if (com2 != -1 && com2 != com) { n           theyknow = false; n         } n       } n     } n   } n   sort(possible.begin(), possible.end()); n   if (possible[0] == possible[possible.size() - 1]) { n     cout << possible[0] << endl; n     return 0; n   } n   if (theyknow) { n     cout << 0 << endl; n   } else { n     cout << -1 << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n bool solve(string temp, string p) { n   int i = 0, j = 0; n   while (i != temp.size()) { n     if (temp[i] == p[j]) { n       i++; n       j++; n     } else n       i++; n     if (j == p.size()) break; n   } n   if (i <= temp.size() && j >= p.size()) n     return true; n   else n     return false; n } n int main() { n   string s, p; n   cin >> s >> p; n   int n = s.size(); n   int arr[n]; n   for (int i = 0; i < n; i++) cin >> arr[i]; n   int lo = 0, hi = n - 1, mid = (hi + lo) / 2; n   while (lo <= hi) { n     string temp = s; n     for (int i = 0; i < mid; i++) temp[arr[i] - 1] =  0 ; n     if (solve(temp, p)) n       lo = mid + 1; n     else n       hi = mid - 1; n     mid = (hi + lo) / 2; n   } n   cout << mid; n } n


#include <bits/stdc++.h> n using namespace std; n int total_operation = 0; n bool is_prime(long long n) { n   if (n == 2) return true; n   if (n % 2 == 0 || n < 2) return false; n   for (int i = 3; i * i <= n; i += 2) { n     if (n % i == 0) return false; n   } n   return true; n } n int countprime_in_range(int n) { n   vector<bool> isprime(n + 1, true); n   isprime[0] = isprime[1] = false; n   for (int i = 2; i * i <= n; ++i) { n     if (isprime[i]) { n       for (int j = 2 * i; j <= n; j += i) { n         isprime[j] = 0; n       } n     } n   } n   int cnt = 0; n   for (int i = 0; i <= n; i++) { n     if (isprime[i]) cnt++; n   } n   return cnt; n } n int main() { n   int fact1[3], fact2[3]; n   fact1[0] = fact1[1] = fact1[2] = 0; n   fact2[0] = fact2[1] = fact2[2] = 0; n   int A, B; n   cin >> A >> B; n   if (A == B) { n     cout <<  0 ; n     return 0; n   } n   while (A) { n     if (!(A % 2)) n       fact1[0]++, A /= 2; n     else if (!(A % 3)) n       fact1[1]++, A /= 3; n     else if (!(A % 5)) n       fact1[2]++, A /= 5; n     else n       break; n   } n   while (B) { n     if (!(B % 2)) n       fact2[0]++, B /= 2; n     else if (!(B % 3)) n       fact2[1]++, B /= 3; n     else if (!(B % 5)) n       fact2[2]++, B /= 5; n     else n       break; n   } n   if (A != B) n     cout <<  -1 ; n   else n     cout << abs(fact1[0] - fact2[0]) + abs(fact1[1] - fact2[1]) + n                 abs(fact1[2] - fact2[2]); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long t; n   cin >> t; n   while (t--) { n     long long n, a, b; n     cin >> n >> a >> b; n     string s =   ; n     char x =  a ; n     for (int i = 0; i < n; i++) { n       if (i % b == 0) x =  a ; n       s += x; n       x++; n     } n     cout << s << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 2e5 + 3; n string s, p; n int F[MAX][2]; n void solve() { n   cin >> s >> p; n   F[0][0] = F[0][1] = 0; n   for (int i = 1; i <= p.size(); ++i) { n     for (int j = 0; j < 2; ++j) { n       F[i][j] = F[i - 1][j]; n     } n     ++F[i][p[i - 1] -  0 ]; n   } n   long long int ans = 0; n   for (int i = 0; i < s.size(); ++i) { n     int c = s[i] -  0 ; n     for (int j = 0; j < 2; ++j) { n       ans += abs(c - j) * (F[p.size() - s.size() + i + 1][j] - F[i][j]); n     } n   } n   cout << ans <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n std::string x; n long long getlen(long long r) { n   long long now = 1, lst = 0, res = 0; n   int dig = 0; n   while (now <= r) { n     res += (now - 1 - lst) * dig; n     lst = now - 1; n     now *= 10; n     ++dig; n   } n   res += (r - lst) * dig; n   return res; n } n int main() { n   long long l = 1, r = 1000000000000ll, ans = 0; n   long long t; n   std::cin >> t; n   while (l <= r) { n     long long mid = l + r >> 1; n     if (getlen(mid) >= t) n       r = mid - 1; n     else n       l = mid + 1, ans = mid; n   } n   t -= getlen(ans); n   std::string st; n   for (long long i = ans + 1; i <= ans + 10; ++i) st += std::to_string(i); n   printf( %c n , st[t - 1]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n vector<pair<int, int>> ans; n int main() { n   cin.tie(0); n   ios::sync_with_stdio(false); n   int n; n   cin >> n; n   vector<int> a(n); n   for (int i = 0; i < n; i++) cin >> a[i]; n   reverse(a.begin(), a.end()); n   vector<pair<int, int>> ans; n   int m = a[0] + 1; n   vector<int> cnt(m); n   int l = 0, r = n; n   int st = 0; n   int en = m; n   while (l < r) { n     if (cnt[en - 1] == a[r - 1]) { n       --en; n       --r; n       continue; n     } n     if (a[l] >= en) l++; n     for (int i = 0; i < a[l] - st; i++) { n       ans.emplace_back(st, st + i + 1); n       cnt[st]++; n       cnt[st + i + 1]++; n     } n     en = a[l] + 1; n     st++; n   } n   cout << ans.size() << endl; n   for (auto p : ans) cout << p.first + 1 <<     << p.second + 1 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int a[1000005], dp1[1000005], dp2[1000005], dp3[1000005]; n long long ans; n int main(int argc, const char* argv[]) { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m; n   for (int i = 1; i <= n; ++i) cin >> a[i]; n   for (int i = 0; i <= m + 1; ++i) { n     dp1[i] = 1e9; n     dp2[i] = -1e9; n   } n   int x; n   int mn = 1e9; n   for (int i = n; i > 0; --i) { n     x = a[i]; n     if (mn < x && mn < dp1[x]) dp1[x] = mn; n     if (x < mn) mn = x; n   } n   int mx = -1e9; n   for (int i = 1; i <= n; ++i) { n     x = a[i]; n     if (mx > x && mx > dp2[x]) dp2[x] = mx; n     if (x > mx) mx = x; n   } n   for (int i = m; i > 0; --i) { n     dp3[i] = dp1[i]; n     if (dp1[i + 1] < dp1[i]) dp1[i] = dp1[i + 1]; n   } n   for (int i = m; i > 0; --i) { n     if (dp2[i + 1] > dp2[i]) dp2[i] = dp2[i + 1]; n   } n   int l, r; n   for (int i = 1; i <= m; ++i) { n     l = i; n     r = m; n     while (l < r) { n       int mid = (l + r) >> 1; n       if (dp1[mid + 1] >= i && dp2[mid + 1] <= mid) n         r = mid; n       else n         l = mid + 1; n     } n     if (dp1[r + 1] >= i && dp2[r + 1] <= r) ans += m - r + 1; n     if (dp3[i] < i) break; n   } n   cout << ans <<   n ; n   return 0; n } n


#include<cstdio> n #include<cstring> n #include<algorithm> n using namespace std; n const int mod=1e9+7; n inline int addmod(int x) n { n  return x>=mod?x-mod:x; n } n inline int submod(int x) n { n  return x<0?x+mod:x;  n } n int n,m,q,b[105],c[105],sum[105],f[105][10005],qans[1005],pn; n int solve(int x) n { n  memset(f,0,sizeof(f)); n  f[0][0]=1; n  int nw=0,ans=0; n  for(int i=1;i<=n;i++) n  { n   for(int j=0;j<=nw;j++) n   { n    f[i][j]=addmod(f[i][j]+f[i-1][j]); n    f[i][j+c[i]+1]=submod(f[i][j+c[i]+1]-f[i-1][j]); n   } n   nw+=c[i]; n   for(int j=1;j<=nw;j++) n    f[i][j]=addmod(f[i][j]+f[i][j-1]); n   for(int j=0;j<x*i+sum[i];j++) n    f[i][j]=0; n  } n  for(int i=0;i<=nw;i++) n   ans=addmod(ans+f[n][i]); n  return ans; n } n int main() n { n  scanf( %d ,&n); n  int mul=1; n  for(int i=1;i<=n;i++) n  { n   scanf( %d ,&c[i]); n   m=max(m,c[i]); n   mul=1ll*mul*(c[i]+1)%mod; n  } n  for(int i=1;i<n;i++) n   scanf( %d ,&b[i]); n  for(int i=1;i<=n;i++) n   for(int j=1;j<i;j++) n    sum[i]+=(i-j)*b[j]; n  pn=(-sum[n])/n; n  for(int i=0;i<=m;i++) n   qans[i]=solve(i+pn); n  scanf( %d ,&q); n  for(int i=1;i<=q;i++) n  { n   int x; n   scanf( %d ,&x); n   if(x<pn) printf( %d n ,mul); n   else if(x>pn+m) printf( 0 n ); n   else printf( %d n ,qans[x-pn]); n  } n  return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   cin.tie(0); n   int i, n, e; n   double x1, y1, x2, y2, vmax, t, vx, vy, wx, wy; n   cin >> x1 >> y1 >> x2 >> y2; n   cin >> vmax >> t; n   cin >> vx >> vy; n   cin >> wx >> wy; n   double sid; n   double l = 0.0, r = (double)1e9; n   double eps = 1e-7; n   double x, y; n   while (r - l > eps) { n     sid = (r + l) / 2.0; n     if (sid < t) { n       x = x1 + sid * vx; n       y = y1 + sid * vy; n     } else { n       x = x1 + t * vx + (sid - t) * wx; n       y = y1 + t * vy + (sid - t) * wy; n     } n     double val; n     val = (x2 - x) * (x2 - x) + (y2 - y) * (y2 - y); n     val = sqrt(val); n     if (val < vmax * sid) { n       r = sid; n     } else n       l = sid; n   } n   cout << setprecision(8); n   cout << fixed; n   cout << r <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void reshenie() { n   int n, m; n   cin >> n >> m; n   int a[n][n]; n   for (int i = 0; i < n; ++i) { n     for (int j = i; j < n; ++j) { n       a[i][j] = a[j][i] = 0; n     } n   } n   while (m--) { n     int u, v; n     cin >> u >> v; n     --u, --v; n     a[u][v] = a[v][u] = 1; n   } n   if (n == 1) { n     cout <<  Yes na  << endl; n     return; n   } n   string s(n,  * ); n   for (int i = 0; i < n; ++i) { n     for (int j = i + 1; j < n; ++j) { n       if (a[i][j]) { n         s[i] = s[j] =  b ; n       } n     } n   } n   for (int i = 0; i < n; ++i) { n     if (accumulate(a[i], a[i] + n, 0) != n - 1) { n       if (s[i] !=  c ) { n         s[i] =  a ; n         for (int j = i + 1; j < n; ++j) { n           if (!a[i][j]) { n             s[j] =  c ; n           } n         } n       } else { n         s[i] =  c ; n         for (int j = i + 1; j < n; ++j) { n           if (!a[i][j]) { n             s[j] =  a ; n           } n         } n       } n     } n   } n   for (int i = 0; i < n; ++i) { n     if (s[i] ==  * ) { n       cout <<  No  << endl; n       return; n     } n     for (int j = i + 1; j < n; ++j) { n       if (a[i][j] && abs(s[i] - s[j]) <= 1) continue; n       if (!a[i][j] && abs(s[i] - s[j]) > 1) continue; n       cout <<  No  << endl; n       return; n     } n   } n   cout <<  Yes n  << s << endl; n } n int main() { n   reshenie(); n   return 1 - 1 + 2 - 2 + 3 - 3 + 4 - 4 + 5 - 5; n } n