#include<bits/stdc++.h> n #define LL long long n #define SZ(x) (int)x.size()-1 n #define F(i,a,b) for (int i=a;i<=b;++i) n #define DF(i,a,b) for (int i=a;i>=b;--i) n #define pb push_back n #define ms(a,b) memset(a,b,sizeof a) n using namespace std; n int read(){ n     char ch=getchar(); int w=1,c=0; n     for (;!isdigit(ch);ch=getchar()) if (ch== - ) w=-1; n     for (;isdigit(ch);ch=getchar()) c=(c<<3)+(c<<1)+(ch^48); n     return w*c; n } n const int M=2e4+10; n int n,a[M],m; n #define lowbit(x) (x&-x) n #define pii pair<int,int> n #define mp make_pair n pii d[M]; n void add(int x,pii y){ n  for (;x<=n;x+=lowbit(x)) d[x]=max(d[x],y); n } n pii qry(int x){ n  pii ret=mp(0,0); n  for (;x;x-=lowbit(x)) ret=max(ret,d[x]); n  return ret; n } n pii f[15][M][33]; n int g[15][M][33],b[M],tw[33],ted[33]; n pii nw[33]; n int main(){ n  n=read(); m=read(); n  F(i,1,n) a[i]=read(),b[i]=min(a[i]+i,n); n  DF(i,n,1){ n   F(j,0,30) g[0][i][j]=min(n,b[i]+j);  n   add(i,mp(b[i],i)); n   f[0][i][0]=qry(b[i]); n   int tmp=f[0][i][0].second; n   F(j,1,30){ n    f[0][i][j]=max(f[0][i][j-1],mp(b[min(b[i]+j,n)],min(b[i]+j,n))); n   } n  } n  F(t,1,14){ n   F(i,1,n){ n    F(j,0,30){ n     F(jj,0,j){ n      int ps=f[t-1][i][jj].second; n      f[t][i][j]=max(f[t][i][j],f[t-1][ps][j-jj]); n      g[t][i][j]=max(g[t][i][j],g[t-1][ps][j-jj]); n     } n    } n   } n  } n  F(T,1,m){ n   int l=read(),r=read(),k=read(); n   if (l==r){ n    cout<<0<<  n ; n    continue; n   } n   F(i,0,k) nw[i]=mp(b[l],l); n   int ans=1e9,cur=0; n   DF(i,14,0){ n    F(j,0,k) tw[j]=nw[j].second,ted[j]=0; n    F(j,0,k){ n     F(jj,0,j){ n      ted[j]=max(ted[j],g[i][tw[jj]][j-jj]);  n     }  n    } n    int mx=0; n    F(j,0,k) mx=max(mx,ted[j]); n    if (mx>=r){ n     ans=min(ans,cur|(1<<i)); n    } n    else{ n     F(j,0,k){ n      F(jj,0,j){ n       nw[j]=max(nw[j],f[i][tw[jj]][j-jj]);  n      }  n     } n     cur|=(1<<i); n    } n   } n   cout<<ans<<  n ; n  } n     return 0; n } n /* n 100 1 n 4 6 5 1 6 6 6 5 3 6 4 4 2 1 6 4 2 1 2 1 4 6 6 5 4 5 5 4 4 5 5 4 1 2 3 1 3 5 4 4 5 3 6 5 5 1 4 4 1 5 5 4 5 1 1 5 3 4 4 2 6 6 4 4 5 2 6 6 2 3 1 6 3 4 4 1 2 1 2 2 3 4 1 3 3 4 6 3 2 5 6 6 6 6 4 6 3 2 1 5 n 19 84 14 n */


#include <bits/stdc++.h> n using namespace std; n long long int mod = 1e8 + 5e4 + 1; n const double error = 1e-7; n const double PI = acos(-1); n mt19937 rng(chrono::system_clock::now().time_since_epoch().count()); n inline long long int MOD(long long int x, long long int m = mod) { n   long long int y = x % m; n   return (y >= 0) ? y : y + m; n } n const int inf = 0x3f3f3f3f; n const unsigned long long int infl = 1e18 + 1; n const int nmax = 2e3 + 5; n typedef long long int T; n bool lexcomp(const complex<T> &p, const complex<T> &q) { n   return make_pair(p.real(), p.imag()) < make_pair(q.real(), q.imag()); n } n complex<T> origin, ray; n inline bool half(complex<T> p) { n   return ((conj(ray) * (p)).imag()) < 0 || n          (((conj(ray) * (p)).imag()) == 0 && ((conj(ray) * (p)).real()) > 0); n } n bool polarComp(complex<T> p, complex<T> q) { n   p -= origin; n   q -= origin; n   return make_tuple(half(p), 0) < make_tuple(half(q), ((conj(p) * (q)).imag())); n } n vector<complex<T> > points; n vector<int> ans; n vector<int> input; n char str[nmax]; n int main() { n   int n; n   cin >> n; n   points.resize(n + 1); n   for (int i = 1; i <= n; i++) { n     int x, y; n     cin >> x >> y; n     points[i] = complex<T>(x, y); n     input.push_back(i); n   } n   cin >> str; n   int pid = n       min_element(input.begin(), input.end(), n                   [&](int i, int j) { return lexcomp(points[i], points[j]); }) - n       input.begin(); n   ans.push_back(input[pid]); n   origin = points[input[pid]]; n   ray = complex<T>(0, -1); n   input.erase(input.begin() + pid); n   for (int i = 0; i < n - 2; i++) { n     if (str[i] ==  L ) n       pid = min_element( n                 input.begin(), input.end(), n                 [&](int i, int j) { return polarComp(points[i], points[j]); }) - n             input.begin(); n     else n       pid = max_element( n                 input.begin(), input.end(), n                 [&](int i, int j) { return polarComp(points[i], points[j]); }) - n             input.begin(); n     ray = -points[ans.back()] + points[input[pid]]; n     ans.push_back(input[pid]); n     origin = points[input[pid]]; n     input.erase(input.begin() + pid); n   } n   ans.push_back(input[0]); n   for (int x : ans) cout << x <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n int N; n vector<int> arr; n int main() { n   cin >> N; n   for (int i = 0; i < N; i++) { n     int num; n     cin >> num; n     arr.push_back(num); n   } n   vector<int> cur(1000, 0); n   int toto = 0; n   for (int i = 0; i < N; i++) { n     int zoto = toto; n     for (int k = 0; k < cur.size(); k++) { n       zoto -= cur[k]; n       int remo = (arr[i] - zoto) / 9 + ((arr[i] - zoto) % 9 ? 1 : 0); n       if (arr[i] - zoto < 0) { n         continue; n       } n       if (k < remo - 1 || cur[k] == 9) n         continue; n       else if (k == remo - 1) { n         int montar = (arr[i] - zoto) % 9; n         if (montar == 0) montar = 9; n         if (zoto + montar + 9 * k > arr[i]) continue; n         montar = max(montar, cur[k] + 1); n         if (zoto + montar > arr[i]) continue; n         toto -= cur[k]; n         cur[k] = montar; n         toto += cur[k]; n       } else { n         if (zoto + cur[k] + 1 > arr[i]) continue; n         cur[k]++; n         toto++; n       } n       int remy = arr[i] - (zoto + cur[k]); n       for (int hu = 0; hu < k; hu++) { n         if (remy >= 9) { n           toto -= cur[hu]; n           cur[hu] = 9; n           toto += cur[hu]; n           remy -= cur[hu]; n         } else if (remy >= 0) { n           toto -= cur[hu]; n           cur[hu] = remy; n           toto += cur[hu]; n           remy -= cur[hu]; n         } else n           break; n       } n       break; n     } n     int maxx = 0; n     for (int x = 0; x < cur.size(); x++) { n       if (cur[x] != 0) maxx = x; n     } n     for (int x = maxx; x >= 0; x--) { n       cout << cur[x]; n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int he[100005], np; n int n; n long long v[100005], f2[100005], fl[100005], ms[100005]; n long long h[100005], g[100005]; n int read() { n   int x = 0; n   char ch; n   ch = getchar(); n   while (ch <  0  || ch >  9 ) ch = getchar(); n   while (ch >=  0  && ch <=  9 ) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x; n } n struct edg { n   int to, nxt; n } e[200005]; n void ad(int x, int y) { n   np++; n   e[np].to = y; n   e[np].nxt = he[x]; n   he[x] = np; n } n void dfs(int x, int fa) { n   f2[x] = fl[x] = ms[x] = g[x] = v[x]; n   for (int i = he[x]; i; i = e[i].nxt) { n     int y = e[i].to; n     if (y == fa) continue; n     dfs(y, x); n     f2[x] = max(f2[x], f2[y]); n     f2[x] = max(f2[x], g[x] + ms[y]); n     f2[x] = max(f2[x], ms[x] + g[y]); n     f2[x] = max(f2[x], fl[x] + fl[y]); n     fl[x] = max(fl[x], ms[x] + ms[y]); n     fl[x] = max(fl[x], fl[y]); n     g[x] = max(g[x], v[x] + g[y]); n     g[x] = max(g[x], ms[y] + v[x] + h[x]); n     g[x] = max(g[x], ms[x] + fl[y]); n     h[x] = max(h[x], fl[y]); n     ms[x] = max(ms[x], v[x] + ms[y]); n   } n } n int main() { n   n = read(); n   for (int i = 1; i <= n; i++) { n     v[i] = read(); n   } n   for (int i = 1; i <= n - 1; i++) { n     int x = read(), y = read(); n     ad(x, y); n     ad(y, x); n   } n   dfs(1, 0); n   cout << f2[1]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 400010; n inline long long read() { n   long long s = 0, w = 1; n   register char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) w = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) n     s = (s << 3) + (s << 1) + (ch ^ 48), ch = getchar(); n   return s * w; n } n void print(long long x) { n   if (x < 0) x = -x, putchar( - ); n   if (x > 9) print(x / 10); n   putchar(x % 10 +  0 ); n } n long long n, k; n long long a[N]; n void MS(long long l, long long r) { n   if (!k || r - l <= 1) return; n   long long mid = (l + r >> 1); n   k -= 2, swap(a[mid - 1], a[mid]); n   MS(l, mid), MS(mid, r); n } n signed main() { n   n = read(), k = read(); n   for (register long long i = 0; i < n; i++) a[i] = i; n   if (!(k & 1ll)) { n     puts( -1 ); n     return 0; n   } n   if (k == 1) { n     for (register long long i = 1; i <= n; i++) printf( %lld  , i); n     puts(  ); n     return 0; n   } n   k--; n   MS(0, n); n   if (!k) { n     for (register long long i = 0; i < n; i++) printf( %lld  , a[i] + 1); n     puts(  ); n   } else n     puts( -1 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long b, d, s, x, y, z, m, t; n   x = y = z = 0; n   cin >> b >> d >> s; n   long long a[3] = {b, d, s}; n   sort(a, a + 3); n   if (a[0] == a[2]) { n     cout << 0; n   } else if (a[1] == a[2]) { n     cout << a[2] - a[0] - 1; n   } else { n     cout << 2 * a[2] - a[0] - a[1] - 2; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long l, r; n   cin >> l >> r; n   while (l % 2) l++; n   if (l + 2 > r) n     cout << -1 << endl; n   else n     cout << l <<     << l + 1 <<     << l + 2 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 1000000007; n int n, k; n vector<vector<long long> > mul(vector<vector<long long> > &A, n                                vector<vector<long long> > &B) { n   vector<vector<long long> > C(1, vector<long long>(n)); n   for (int i = 0; i < n; ++i) { n     for (int k = 0; k <= i; ++k) { n       C[0][i] = (C[0][i] + A[0][k] * B[0][i - k]) % M; n     } n   } n   return C; n } n vector<vector<long long> > pow(vector<vector<long long> > A, int k) { n   vector<vector<long long> > B(1, vector<long long>(n)); n   B[0][0] = 1; n   while (k > 0) { n     if (k & 1) B = mul(B, A); n     A = mul(A, A); n     k >>= 1; n   } n   return B; n } n void solve() { n   vector<vector<long long> > f(1, vector<long long>(n)); n   for (int i = 0; i < n; ++i) { n     f[0][i] = 1; n   } n   f = pow(f, k); n   vector<vector<long long> > A(1, vector<long long>(n)); n   for (int i = 0; i < n; ++i) cin >> A[0][i]; n   cout << A[0][0]; n   for (int i = 1; i < n; ++i) { n     long long tmp = 0; n     for (int j = 0; j <= i; ++j) { n       tmp = (tmp + A[0][j] * f[0][i - j]) % M; n     } n     cout <<     << tmp; n   } n } n int main() { n   scanf( %d%d , &n, &k); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Mod = 1e9 + 7; n int n, m, u, f[3100][3100], sum[3100][3100], C[3100][3100], inv[3100], g[3100]; n int edgenum, vet[210000], Next[210000], Head[110000]; n void addedge(int u, int v) { n   vet[++edgenum] = v; n   Next[edgenum] = Head[u]; n   Head[u] = edgenum; n } n void dfs(int u) { n   for (int i = Head[u]; i; i = Next[i]) { n     dfs(vet[i]); n     for (int j = 1; j <= n; j++) f[u][j] = 1ll * f[u][j] * sum[vet[i]][j] % Mod; n   } n   for (int i = 1; i <= n; i++) sum[u][i] = (sum[u][i - 1] + f[u][i]) % Mod; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 2; i <= n; i++) { n     scanf( %d , &u); n     addedge(u, i); n   } n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) f[i][j] = 1; n   dfs(1); n   C[0][0] = 1; n   for (int i = 1; i <= n; i++) { n     C[i][0] = 1; n     for (int j = 1; j <= i; j++) n       C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % Mod; n   } n   inv[0] = 1; n   inv[1] = 1; n   for (int i = 2; i <= n; i++) n     inv[i] = 1ll * (Mod - Mod / i) * inv[Mod % i] % Mod; n   for (int i = 2; i <= n; i++) inv[i] = 1ll * inv[i - 1] * inv[i] % Mod; n   for (int i = 1; i <= n; i++) { n     g[i] = f[1][i]; n     for (int j = 1; j < i; j++) n       g[i] = (g[i] - 1ll * C[i - 1][j - 1] * g[j] % Mod + Mod) % Mod; n   } n   int ans = 0; n   for (int i = 1; i <= n && i <= m; i++) { n     int mul = inv[i]; n     for (int j = m - i + 1; j <= m; j++) mul = 1ll * mul * j % Mod; n     ans = (ans + 1ll * mul * g[i] % Mod) % Mod; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long mod = 1000000007; n long long N = mod; n long long quick_mod(long long a, long long b) { n   long long ans = 1; n   a %= N; n   while (b) { n     if (b % 2 != 0) { n       ans = ans * a % N; n       b--; n     } n     b /= 2; n     a = a * a % N; n   } n   return ans % N; n } n int main() { n   long long m, n, i, j, k; n   while (cin >> m) { n     if (m == 0) { n       cout << 1 << endl; n       continue; n     } n     cout << (quick_mod(2, m - 1) * (1 + quick_mod(2, m))) % N << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n template <int modd> n struct modint { n   static const int mod = modd; n   int v; n   explicit operator int() const { return v; } n   modint() : v(0) {} n   modint(long long x) : v(int(x % mod)) { n     if (v < 0) v += mod; n   } n   friend bool operator==(const modint& a, const modint& b) { n     return a.v == b.v; n   } n   friend bool operator!=(const modint& a, const modint& b) { n     return a.v != b.v; n   } n   friend bool operator<(const modint& a, const modint& b) { return a.v < b.v; } n   modint& operator+=(const modint& a) { n     if ((v += a.v) >= mod) v -= mod; n     return *this; n   } n   modint& operator-=(const modint& a) { n     if ((v -= a.v) < 0) v += mod; n     return *this; n   } n   modint& operator*=(const modint& a) { n     v = int((long long)v * a.v % mod); n     return *this; n   } n   modint& operator/=(const modint& a) { return (*this) *= inv(a); } n   friend modint pow(modint a, long long p) { n     modint ret = 1; n     for (; p; p /= 2, a *= a) { n       if (p & 1) ret *= a; n     } n     return ret; n   } n   friend modint inv(const modint& a) { return pow(a, mod - 2); } n   modint operator-() const { return modint(-v); } n   modint& operator++() { return *this += 1; } n   modint& operator--() { return *this -= 1; } n   friend modint operator+(modint a, const modint& b) { return a += b; } n   friend modint operator-(modint a, const modint& b) { return a -= b; } n   friend modint operator*(modint a, const modint& b) { return a *= b; } n   friend modint operator/(modint a, const modint& b) { return a /= b; } n   friend ostream& operator<<(ostream& out, modint a) { return out << a.v; } n   friend istream& operator>>(istream& in, modint& a) { n     long long b; n     in >> b; n     a.v = b; n     return in; n   } n }; n using mint = modint<1000000007>; n struct change { n   int* ptr; n   int val, t; n   change(int* p, int v, int tin) : ptr(p), val(v), t(tin) {} n   void undo() { *ptr = val; } n }; n struct dsu { n   vector<int> v, len; n   stack<change> st; n   int ttime, ct; n   dsu(int n) : v(n), len(n, 1), ttime(0), ct(n) { iota(v.begin(), v.end(), 0); } n   void make_change(int* p, int val) { n     change ch(p, *p, ttime); n     st.push(ch); n     *p = val; n   } n   void batch_undo() { n     assert(!st.empty()); n     int tt = st.top().t; n     while (((int)((st).size())) && st.top().t == tt) { n       st.top().undo(); n       st.pop(); n     } n   } n   int root(int a) { return (v[a] == a ? a : root(v[a])); } n   bool unite(int a, int b) { n     ttime++; n     int a1 = root(a), b1 = root(b); n     if (a1 == b1) { n       make_change(&v[0], v[0]); n       return false; n     } n     make_change(&ct, ct - 1); n     if (len[a1] < len[b1]) swap(a1, b1); n     make_change(&len[a1], len[a1] + len[b1]); n     make_change(&v[b1], a1); n     return true; n   } n }; n const int mx = 5e5 + 5; n int n, m, k; n long long a[mx]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> m >> k; n   for (int q = 1; q <= n; q++) cin >> a[q]; n   vector<array<long long, 3>> v(m); n   for (int q = 0; q < m; q++) { n     cin >> v[q][1] >> v[q][2]; n     v[q][0] = a[v[q][1]] ^ a[v[q][2]]; n   } n   sort(v.begin(), v.end()); n   int q = 0, tot = 0; n   dsu d(n + 1); n   mint ans = 0; n   while (q < m) { n     tot++; n     int w = q; n     while (w < m && v[w][0] == v[q][0]) { n       d.unite(v[w][1], v[w][2]); n       w++; n     } n     ans += pow(mint(2), d.ct - 1); n     w = q; n     while (w < m && v[w][0] == v[q][0]) { n       d.batch_undo(); n       w++; n     } n     q = w; n   } n   ans += (pow(mint(2), k) - tot) * pow(mint(2), n); n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n; n   cin >> n; n   vector<int> v; n   int temp = 0; n   for (int i = 0; i < n; i++) { n     cin >> temp; n     v.push_back(temp); n   } n   int dem0 = 0; n   if (n == 1) { n     if (v[0] == 1) n       cout <<  YES ; n     else n       cout <<  NO ; n   } else { n     for (int i = 0; i < n; i++) { n       if (v[i] == 0) dem0++; n     } n     if (dem0 == 0 || dem0 > 1) n       cout <<  NO ; n     else n       cout <<  YES ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3E6 + 5; n const int M = 3E6 + 5; n struct map { n   int l, r; n } Q[M]; n int n, m, k, len, tr[N * 4], u, x, P[N], tot; n char s[N], ans[N], ch; n void add(int p, int l, int r, int x) { n   if (x < l || x > r) return; n   tr[p]++; n   if (l == r) return; n   int mid = (l + r) / 2; n   add(p * 2, l, mid, x); n   add(p * 2 + 1, mid + 1, r, x); n } n int get(int x) { n   for (int p = 1, l = 1, r = k;;) { n     if (l == r) return x == 1 && !tr[p] ? l : k + 1; n     int mid = (l + r) / 2; n     if (mid - l + 1 - tr[p * 2] < x) { n       x -= mid - l + 1 - tr[p * 2]; n       l = mid + 1; n       p = p * 2 + 1; n     } else n       r = mid, p *= 2; n   } n } n int main() { n   while (ch = getchar(), ch !=   n  && tot < N) s[tot++] = ch; n   if (ch !=   n ) n     while (ch = getchar(), ch !=   n ) n       ; n   scanf( %d%d , &k, &n); n   for (int i = 1; i <= n; i++) scanf( %d%d , &Q[i].l, &Q[i].r); n   for (int i = n; i; i--) { n     len = Q[i].r - Q[i].l + 1; n     u = 1; n     for (int j = Q[i].l + 1; j <= Q[i].r && Q[i].r + u <= k; j += 2) { n       x = get(Q[i].r + 1); n       if (x > k) break; n       P[x] = get(j); n       u++; n       add(1, 1, k, x); n     } n     for (int j = Q[i].l; j <= Q[i].r && Q[i].r + u <= k; j += 2) { n       x = get(Q[i].r + 1); n       if (x > k) break; n       P[x] = get(j); n       u++; n       add(1, 1, k, x); n     } n   } n   u = 0; n   for (int i = 1; i <= k; i++) { n     if (!P[i]) n       ans[i] = s[u++]; n     else n       ans[i] = ans[P[i]]; n     putchar(ans[i]); n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long MOD = 1000000007; n long double EPS = 1e-9; n long long binpow(long long b, long long p, long long mod) { n   long long ans = 1; n   b %= mod; n   for (; p; p >>= 1) { n     if (p & 1) ans = ans * b % mod; n     b = b * b % mod; n   } n   return ans; n } n void pre() {} n long long arr[1010][1010]; n int row[1010][1010][2]; n int col[1010][1010][2]; n void solve() { n   long long n, m; n   cin >> n >> m; n   for (long long i = 0; i < (n); ++i) { n     for (long long j = 0; j < (m); ++j) { n       cin >> arr[i][j]; n     } n   } n   for (long long i = 0; i < (n); ++i) { n     set<long long> st; n     vector<long long> temp; n     for (long long j = 0; j < (m); ++j) { n       st.insert(arr[i][j]); n     } n     temp = vector<long long>((st).begin(), (st).end()); n     for (long long j = 0; j < (m); ++j) { n       row[i][j][0] = n           lower_bound((temp).begin(), (temp).end(), arr[i][j]) - temp.begin(); n       row[i][j][1] = n           upper_bound((temp).begin(), (temp).end(), arr[i][j]) - temp.begin(); n       row[i][j][1] = (long long)(temp).size() - row[i][j][1]; n     } n   } n   for (long long j = 0; j < (m); ++j) { n     set<long long> st; n     vector<long long> temp; n     for (long long i = 0; i < (n); ++i) { n       st.insert(arr[i][j]); n     } n     temp = vector<long long>((st).begin(), (st).end()); n     for (long long i = 0; i < (n); ++i) { n       col[i][j][0] = n           lower_bound((temp).begin(), (temp).end(), arr[i][j]) - temp.begin(); n       col[i][j][1] = n           upper_bound((temp).begin(), (temp).end(), arr[i][j]) - temp.begin(); n       col[i][j][1] = (long long)(temp).size() - col[i][j][1]; n     } n   } n   for (long long i = 0; i < (n); ++i) { n     for (long long j = 0; j < (m); ++j) { n       cout << max(row[i][j][0], col[i][j][0]) + 1 + n                   max(row[i][j][1], col[i][j][1]) n            <<    ; n     } n     cout <<   n ; n   } n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   pre(); n   long long t = 1; n   for (long long i = 1; i <= t; i++) { n     solve(); n   } n } n


#include<bits/stdc++.h> n #define ll long long n using namespace std; n  n void solve(int tc) { n  int n; n  cin >> n; n  string s; n  cin >> s; n  vector<int> pos; n  for (int i = 0  ; i < n ; i++) { n   char ch = s[i]; n   if (ch ==  * ) pos.push_back(i); n  } n  n  int sz = (int)pos.size(); n  if (sz == 0) { n   cout << 0 << endl; n   return; n  } n  n  int median = sz / 2; n  ll ans = 0; n  for (int i = median - 1 , j = pos[median] - 1 ; i >= 0 ; i-- , j--) { n   ans += abs(pos[i] - j); n  } n  n  for (int i = median + 1 , j = pos[median] + 1 ; i < sz ; i++ , j++) { n   ans += abs(pos[i] - j); n  } n  n  cout << ans << endl; n } n  n int main() n { n  int t; n  cin >> t; n  n  while (t--) n   solve(0); n }


#include<bits/stdc++.h> n  n #define ld long double n #define ll long long n using namespace std; n template<class T> n void read(T& x) n { n  T res = 0, f = 1; char c = getchar(); n  while (!isdigit(c)) { n   if (c ==  - )f = -1; c = getchar(); n  } n  while (isdigit(c)) { n   res = (res << 3) + (res << 1) + c -  0 ; c = getchar(); n  } n  x = res * f; n } n const ll N = 200000 + 10; n const int mod = 1e9 + 7; n  n struct seg n { n #define ls(x) (x<<1) n #define rs(x) (x<<1|1) n #define mid (l+r)/2 n  int mn[N << 2],n,a[N],tag[N<<2]; n  void init(int x,int *dp) n  { n   n = x; n   for (int i = 1; i <= n; i++)a[i] = dp[i]; n   build(1, 1, n); n  } n  void push_up(int p) n  { n   mn[p] = min(mn[ls(p)], mn[rs(p)]); n  } n  void build(int p, int l, int r) n  { n   if (l == r) n   { n    mn[p] = a[l]; n    return; n   } n   build(ls(p), l, mid); n   build(rs(p), mid + 1, r); n   push_up(p); n  } n  n  int query(int p, int l, int r, int nl,int nr) n  { n   if (l >nr || nl>r)return 1e9; n   if (nl<=l&&r<=nr) n   { n    return mn[p]; n   } n   n   return  min(query(ls(p), l, mid, nl,nr), query(rs(p), mid + 1, r, nl,nr)); n  } n }seg; n int n[5],dp[5][N]; n int w[5][N]; n vector<int>p[5][N]; n int main() n { n  //ios::sync_with_stdio(false); n #ifndef ONLINE_JUDGE n  freopen( test.in ,  r , stdin); n #endif // ONLINE_JUDGE n  for (int i = 1; i <= 4; i++)read(n[i]); n  for (int i = 1; i <= 4; i++) n   for (int j = 1; j <= n[i]; j++) n    read(w[i][j]); n  for (int i = 2; i <= 4; i++) n  { n   int m; read(m); n   for (int j = 1; j <= n[i]; j++)p[i][j].push_back(0); n   for (int j = 1; j <= m; j++) n   { n    int x, y; read(x), read(y); n    p[i][y].push_back(x); n   } n   for (int j = 1; j <= n[i]; j++)p[i][j].push_back(n[i-1]+1); n  } n  memset(dp, 0x3f, sizeof(dp)); n  for (int i = 1; i <= n[1]; i++)dp[1][i] = w[1][i]; n  n  for (int i = 2; i <= 4; i++) n  { n   seg.init(n[i-1],dp[i-1]); n   for (int j = 1; j <= n[i]; j++)sort(p[i][j].begin(), p[i][j].end()); n    n   for (int j =1; j <=n[i]; j++) n   { n    for (int k = 0; k + 1 < p[i][j].size(); k++) n    { n     dp[i][j] = min(dp[i][j], w[i][j] + seg.query(1, 1, n[i - 1], p[i][j][k] + 1, p[i][j][k + 1] - 1)); n    } n   } n    n  } n  int ans = 1e9; n  for (int i = 1; i <= n[4]; i++)ans = min(ans, dp[4][i]); n  printf( %d n , ans==1e9?-1:ans); n  return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void inp(long long &n, long long &m, vector<long long> &v, n          vector<pair<signed, signed> > &rope) { n   cin >> n >> m; n   v.resize(n + 1); n   rope.resize(m + 1); n   for (long long i = 1; i <= n; ++i) { n     cin >> v[i]; n   } n   for (long long i = 1; i <= m; ++i) { n     cin >> rope[i].first >> rope[i].second; n   } n } n void solve() { n   long long n, m, res = 0; n   vector<long long> v; n   vector<pair<signed, signed> > rope; n   inp(n, m, v, rope); n   for (long long i = 1; i <= m; ++i) { n     res += min(v[rope[i].first], v[rope[i].second]); n   } n   cout << res; n } n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool agm(pair<int, int> a, pair<int, int> b) { n   if (a.first == b.first) { n     if (a.second > b.second) n       return true; n     else n       return false; n   } else if (a.first < b.first) n     return true; n   else n     return false; n } n void solve(vector<int> &v, int sz) { n   int l = 0, idx, r = sz - 1; n   idx = max_element(v.begin(), v.end()) - v.begin(); n   vector<pair<int, int> > vec; n   for (int i = idx; i <= r; i++) { n     vec.push_back(make_pair(v[i], i)); n   } n   sort(vec.begin(), vec.end(), agm); n   reverse(vec.begin(), vec.end()); n   l = vec[0].second; n   printf( %c , vec[0].first +  a ); n   for (int i = 0; i < vec.size(); i++) { n     if (vec[i].second > l) { n       l = vec[i].second; n       printf( %c , vec[i].first +  a ); n     } n   } n } n int main() { n   string s; n   cin >> s; n   int sz = s.length(); n   vector<int> v(sz, 0); n   for (int i = 0; i < sz; i++) v[i] = s[i] -  a ; n   solve(v, sz); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   cout <<  INTERCAL ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = -1; n const long long INF = 1e9 + 19; n int main() { n   int n, m, x; n   scanf( %d%d%d , &n, &m, &x); n   x--; n   auto f = [&](int y) { n     int nn = n - y * 2; n     int mm = m - y * 2; n     if (nn <= 0 || mm <= 0) { n       return 0; n     } n     return (nn * mm + 1) / 2; n   }; n   cout << f(x) - f(x + 1) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int lft[100005], rght[100005], up[100005], dwn[100005]; n int end() { n   cin.get(); n   cin.get(); n   return 0; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int d, n, m, x1[100005], x2[100005], y1[100005], y2[100005], cntl, cntr, cntu, n       cntd; n   cin >> d >> n >> m; n   for (int i(0); i < d; ++i) { n     cin >> x1[i] >> y1[i] >> x2[i] >> y2[i]; n     lft[min(x1[i], x2[i])]++; n     rght[max(x1[i], x2[i])]++; n     up[min(y1[i], y2[i])]++; n     dwn[max(y1[i], y2[i])]++; n   } n   for (int i(1); i <= n; ++i) lft[i] += lft[i - 1]; n   for (int i(n); i >= 1; --i) rght[i] += rght[i + 1]; n   for (int i(1); i <= m; ++i) up[i] += up[i - 1]; n   for (int i(m); i >= 1; --i) dwn[i] += dwn[i + 1]; n   cin >> cntl >> cntr >> cntu >> cntd; n   for (int i(0); i < d; ++i) { n     if (y1[i] == y2[i]) { n       if (cntl == lft[min(x1[i], x2[i])] - 1 && n           cntr == rght[max(x1[i], x2[i])] - 1 && cntu == up[y1[i] - 1] && n           cntd == dwn[y1[i] + 1]) { n         cout << i + 1; n         return end(); n       } n     } else { n       if (cntl == lft[x1[i] - 1] && cntr == rght[x1[i] + 1] && n           cntu == up[min(y1[i], y2[i])] - 1 && n           cntd == dwn[max(y1[i], y2[i])] - 1) { n         cout << i + 1; n         return end(); n       } n     } n   } n   cout << -1; n   cin.get(); n   cin.get(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int OO = 1e9; n const double EPS = 1e-9; n template <class T> n void _db(const char* dbStr, T e) { n   cout << dbStr <<   =   << e << endl; n } n template <class T, class... L> n void _db(const char* dbStr, T e, L... r) { n   while (*dbStr !=  , ) cout << *dbStr++; n   cout <<   =   << e <<  , ; n   _db(dbStr + 1, r...); n } n template <class S, class T> n ostream& operator<<(ostream& o, const map<S, T>& v) { n   o <<  [ ; n   int i = 0; n   for (const pair<S, T>& pr : v) n     o << (!i++ ?    :  ,  ) <<  {  << pr.first <<   :   << pr.second <<  } ; n   return o <<  ] ; n } n template <template <class, class...> class S, class T, class... L> n ostream& operator<<(ostream& o, const S<T, L...>& v) { n   o <<  [ ; n   int i = 0; n   for (const auto& e : v) o << (!i++ ?    :  ,  ) << e; n   return o <<  ] ; n } n template <class S, class T> n ostream& operator<<(ostream& o, const pair<S, T>& pr) { n   return o <<  (  << pr.first <<  ,   << pr.second <<  ) ; n } n ostream& operator<<(ostream& o, const string& s) { n   for (const char& c : s) o << c; n   return o; n } n template <class T> n using V = vector<T>; n template <class T> n using VV = V<V<T>>; n template <class T> n using VVV = VV<V<T>>; n using ll = long long; n using pii = pair<int, int>; n using vi = V<int>; n using vii = V<pii>; n using vvi = VV<int>; n using mii = map<int, int>; n using umii = unordered_map<int, int>; n using si = set<int>; n using usi = unordered_set<int>; n const int MX = 2e5 + 5; n int N, M; n ll s[5]; n vvi A, B; n ll sumR[MX], sumC[MX]; n int main() { n   ios::sync_with_stdio(false); n   cout.precision(10); n   cin.tie(0); n   cin >> N >> M; n   A.assign(N, vi(M)); n   for (int i = 0; i < N; ++i) { n     for (int j = 0; j < M; ++j) { n       cin >> A[i][j]; n     } n   } n   if (max(N, M) <= 2) { n     ll ans = 0; n     for (int i = 0; i < N; ++i) { n       for (int j = 0; j < M; ++j) { n         ans += A[i][j]; n       } n     } n     cout << ans <<   n ; n     return 0; n   } n   if (N > M) { n     B = A; n     A.assign(M, vi(N)); n     for (int i = 0; i < N; ++i) { n       for (int j = 0; j < M; ++j) { n         A[j][i] = B[i][j]; n       } n     } n     swap(N, M); n   } n   for (int i = 0; i < N; ++i) { n     for (int j = 0; j < M; ++j) { n       sumR[i] += A[i][j]; n       sumC[j] += A[i][j]; n     } n   } n   ll ans = 0; n   ll cand = 0; n   if (N >= 4) { n     for (int i = 0; i < N; ++i) { n       s[0] = sumR[i]; n       sort(s, s + 5); n     } n     for (int i = 1; i < 5; ++i) { n       cand += s[i]; n     } n     ans = max(ans, cand); n   } n   true; n   s[0] = s[1] = s[2] = s[3] = s[4] = 0; n   if (M >= 4) { n     for (int i = 0; i < M; ++i) { n       s[0] = sumC[i]; n       sort(s, s + 5); n     } n     cand = 0; n     for (int i = 1; i < 5; ++i) { n       cand += s[i]; n     } n   } n   ans = max(ans, cand); n   true; n   if (M >= 3) { n     for (int i = 0; i < N; ++i) { n       s[0] = s[1] = s[2] = s[3] = 0; n       for (int j = 0; j < M; ++j) { n         s[0] = sumC[j] - A[i][j]; n         sort(s, s + 4); n       } n       cand = 0; n       for (int j = 1; j < 4; ++j) { n         cand += s[j]; n       } n       ans = max(ans, sumR[i] + cand); n     } n   } n   true; n   if (N >= 3) { n     for (int i = 0; i < M; ++i) { n       s[0] = s[1] = s[2] = s[3] = 0; n       for (int j = 0; j < N; ++j) { n         s[0] = sumR[j] - A[j][i]; n         sort(s, s + 4); n       } n       cand = 0; n       for (int j = 1; j < 4; ++j) { n         cand += s[j]; n       } n       ans = max(ans, sumC[i] + cand); n     } n   } n   true; n   s[0] = s[1] = s[2] = 0; n   { n     for (int i = 0; i < N; ++i) { n       for (int j = i + 1; j < N; ++j) { n         s[0] = s[1] = s[2] = 0; n         cand = 0; n         for (int k = 0; k < M; ++k) { n           s[0] = sumC[k] - A[i][k] - A[j][k]; n           sort(s, s + 3); n         } n         ans = max(ans, sumR[i] + sumR[j] + s[1] + s[2]); n       } n     } n   } n   true; n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string solve(int n) { n   if (n == 1) { n     return  -1 ; n   } else { n     return  2  + string(n - 1,  3 ); n   } n } n int main() { n   ios::sync_with_stdio(0), cin.tie(0); n   int T; n   cin >> T; n   while (T--) { n     int n; n     cin >> n; n     cout << solve(n) <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int rj = 1000000000; n   int n, m, a, b; n   scanf( %d %d %d %d , &n, &m, &a, &b); n   for (int i = 0; i <= n; i++) n     for (int j = 0; j <= n; j++) { n       if (i + j * m < n) continue; n       rj = min(rj, a * i + b * j); n     } n   printf( %d n , rj); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const double Pi = acos(-1); n namespace { n template <typename T> n inline void read(T &x) { n   x = 0; n   T f = 1; n   char s = getchar(); n   for (; !isdigit(s); s = getchar()) n     if (s ==  - ) f = -1; n   for (; isdigit(s); s = getchar()) x = (x << 3) + (x << 1) + (s ^ 48); n   x *= f; n } n }  // namespace n long long fun(long long x) { n   switch (x % 4) { n     case 0: n       return x; n     case 1: n       return 1; n     case 2: n       return x + 1; n     case 3: n       return 0; n   } n } n int main() { n   int n; n   read(n); n   long long xi, mi; n   long long ans = 0; n   for (int i = 1; i <= n; i++) { n     read(xi); n     read(mi); n     ans ^= fun(xi + mi - 1) ^ fun(xi - 1); n   } n   if (ans) n     cout <<  tolik  << endl; n   else n     cout <<  bolik  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n struct point { n   double x, y; n   point(){}; n   point(double _x, double _y) { x = _x, y = _y; } n }; n point s, e; n double det(double a, double b, double c, double d) { return (a * d - b * c); } n point perpotongan(point p1, point p2, point p3, point p4) { n   point has; n   has.x = -137; n   has.y = -1134; n   double d1 = det(p1.x, p1.y, p2.x, p2.y); n   double d2 = det(p1.x, 1, p2.x, 1); n   double d3 = det(p3.x, p3.y, p4.x, p4.y); n   double d4 = det(p3.x, 1, p4.x, 1); n   double d5 = det(p1.x, 1, p2.x, 1); n   double d6 = det(p1.y, 1, p2.y, 1); n   double d7 = det(p3.x, 1, p4.x, 1); n   double d8 = det(p3.y, 1, p4.y, 1); n   double top = det(d1, d2, d3, d4); n   double bot = det(d5, d6, d7, d8); n   if (bot == 0) return (has); n   has.x = top / bot; n   d2 = det(p1.y, 1, p2.y, 1); n   d4 = det(p3.y, 1, p4.y, 1); n   top = det(d1, d2, d3, d4); n   has.y = top / bot; n   return (has); n } n double dist(point p1, point p2) { return hypot(p1.x - p2.x, p1.y - p2.y); } n int main() { n   scanf( %lf %lf , &s.x, &s.y); n   scanf( %lf %lf , &e.x, &e.y); n   int n; n   scanf( %d , &n); n   double a, b, c; n   point p, q; n   int ans = 0; n   for (int i = 1; i <= n; i++) { n     scanf( %lf %lf %lf , &a, &b, &c); n     double kiri = a * s.x + b * s.y + c; n     double kanan = a * e.x + b * e.y + c; n     if (((kiri > 0) && (kanan < 0)) || ((kiri < 0) && (kanan > 0))) ++ans; n   } n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[301][301], dp[301][301][301]; n int solve(int i, int j, int k) { n   if (i == n && j == n && k == 0) return a[n][n]; n   int x = i + k, y = j - k; n   if (x < 1 || y < 1 || x > n || y > n || i < 1 || j < 1 || i > n || j > n || n       k < 0) n     return -1e9; n   if (dp[i][j][k] != -2.1e9) return dp[i][j][k]; n   return dp[i][j][k] = a[i][j] + (k != 0) * a[x][y] + n                        max(max(solve(i + 1, j, k), solve(i + 1, j, k - 1)), n                            max(solve(i, j + 1, k + 1), solve(i, j + 1, k))); n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= n; ++j) cin >> a[i][j]; n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= n; ++j) n       for (int k = 0; k <= n; ++k) dp[i][j][k] = -2.1e9; n   cout << solve(1, 1, 0); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   string s; n   cin >> n >> s; n   for (int i = 0; i < n; ++i) n     for (int interval = 1; interval <= n; ++interval) { n       int cnt = 0, cur = i; n       while (cur < n && s[cur] ==  * ) ++cnt, cur = cur + interval; n       if (cnt >= 5) goto END; n     } n   cout <<  no ; n   return 0; n END: n   cout <<  yes ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s = n      What are you doing at the end of the world? Are you busy? Will you save   n      us? ; n string s1 =  What are you doing while sending   ; n string s2 =   ? Are you busy? Will you send   ; n string s3 =   ? ; n const int MAX_N = 1e5 + 10; n const long long MAX_F = 1e18 + 10; n long long F[MAX_N] = {(int)s.size()}; n char f(int n, long long k) { n   if (F[n] <= k) return  . ; n   if (n == 0) return s[k]; n   if (k < (int)s1.size()) return s1[k]; n   k -= (int)s1.size(); n   if (k < F[n - 1]) return f(n - 1, k); n   k -= F[n - 1]; n   if (k < (int)s2.size()) return s2[k]; n   k -= (int)s2.size(); n   if (k < F[n - 1]) return f(n - 1, k); n   k -= F[n - 1]; n   return s3[k]; n } n int main() { n   ios_base ::sync_with_stdio(false), cout.tie(0), cin.tie(0); n   int l = (int)s1.size() + (int)s2.size() + (int)s3.size(); n   for (int i = 1; i < MAX_N; i++) { n     if (2 * F[i - 1] + l > MAX_F) n       F[i] = MAX_F; n     else n       F[i] = 2 * F[i - 1] + l; n   } n   int q; n   cin >> q; n   while (q--) { n     int n; n     long long k; n     cin >> n >> k; n     cout << f(n, k - 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0), cout.tie(0); n   string a, s; n   cin >> a >> s; n   vector<int> v(10, 0); n   for (int i = 0; i < s.size(); i++) v[s[i] - 48]++; n   int j = 9; n   for (int i = 0; i < a.size(); i++) { n     while (v[j] == 0 and j > 0) j--; n     if (j > a[i] - 48) { n       a[i] = j + 48; n       v[j]--; n     } n   } n   cout << a << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n char cmd[(100000 + 10)]; n int vis[510][510], path[(100000 + 10)]; n const int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0}; n int convert(char ch) { n   if (ch ==  R ) n     return 0; n   else if (ch ==  L ) n     return 1; n   else if (ch ==  D ) n     return 2; n   else n     return 3; n } n bool Inborder(int x, int y) { n   if (x < 0 || x >= n || y < 0 || y >= m) return false; n   return true; n } n int main() { n   int x0, y0; n   scanf( %d%d%d%d , &n, &m, &x0, &y0); n   scanf( %s , cmd); n   memset(vis, 0, sizeof(vis)); n   int cnt = 0, pos = 0, len = strlen(cmd), ans = n * m; n   path[cnt++] = 1; n   ans--; n   int x = x0 - 1, y = y0 - 1; n   vis[x][y] = 1; n   while (pos < len - 1) { n     int dir = convert(cmd[pos++]); n     if (!Inborder(dx[dir] + x, dy[dir] + y)) { n       path[cnt++] = 0; n       continue; n     } n     x += dx[dir]; n     y += dy[dir]; n     if (vis[x][y]) { n       path[cnt++] = 0; n     } else { n       ans--; n       path[cnt++] = 1; n       vis[x][y] = 1; n     } n   } n   for (int i = 0; i < cnt; i++) { n     printf( %d  , path[i]); n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int q; n   scanf( %d , &q); n   while (q--) { n     int n; n     scanf( %d , &n); n     int cnt = 0; n     int aa[1000]; n     for (int i = 1; i <= n; i++) { n       scanf( %d , &aa[i]); n     } n     sort(aa + 1, aa + 1 + n); n     int flag = 1; n     for (int i = 2; i <= n; i++) { n       if (aa[i] == aa[i - 1] + 1) { n         flag = 0; n       } n     } n     if (!flag) n       printf( 2 n ); n     else n       printf( 1 n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct point { n   long long x, y; n }; n istream &operator>>(istream &in, point &p) { n   in >> p.x >> p.y; n   return in; n } n ostream &operator<<(ostream &out, point &p) { n   out << p.x <<     << p.y; n   return out; n } n double len(struct point a, struct point b) { n   return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); n } n int main() { n   ios_base::sync_with_stdio(NULL); n   cin.tie(NULL); n   cout.tie(NULL); n   long long r; n   point a, b; n   cin >> r; n   cin >> a >> b; n   cout << ceil(len(a, b) / (r * 2)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<string, long long> m; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   vector<pair<long long, long long> > v = { n       make_pair(1989, 1998),           make_pair(1999, 2098), n       make_pair(2099, 3098),           make_pair(3099, 13098), n       make_pair(13099, 113098),        make_pair(113099, 1113098), n       make_pair(1113099, 11113098),    make_pair(11113099, 111113098), n       make_pair(111113099, 1111113098)}; n   for (int i = 1989; i <= 1998; i++) { n     string s =   ; n     long long temp = i; n     s += (temp % 10 + 48); n     m[s] = i; n   } n   for (int i = 1999; i <= 2098; i++) { n     string s =   ; n     long long temp = i; n     for (int j = 0; j < 2; j++) { n       s += (temp % 10 + 48); n       temp /= 10; n     } n     reverse(s.begin(), s.end()); n     m[s] = i; n   } n   for (int i = 2099; i <= 3098; i++) { n     string s =   ; n     long long temp = i; n     for (int j = 0; j < 3; j++) { n       s += (temp % 10 + 48); n       temp /= 10; n     } n     reverse(s.begin(), s.end()); n     m[s] = i; n   } n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     string s; n     cin >> s; n     string work =   ; n     int sz = s.size(); n     for (int j = 4; j < sz; j++) { n       work += s[j]; n     } n     sz = work.size(); n     if (sz <= 3) { n       cout << m[work] <<   n ; n     } else { n       long long temp = v[sz - 1].first; n       string s1 =   ; n       while (temp) { n         s1 += (temp % 10 + 48); n         temp /= 10; n       } n       reverse(s1.begin(), s1.end()); n       if (work < s1) { n         cout << 1 << work <<   n ; n       } else { n         cout << work <<   n ; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, k, res; n   scanf( %lld %lld , &n, &k); n   if (k <= n) { n     if (k % 2 == 0) n       res = (k - 1) / 2; n     else if (k % 2 == 1) n       res = k / 2; n   } else if (k > n) { n     if (k - n <= n) n       res = (n - (k - n) + 1) / 2; n     else if (k - n > n) n       res = 0; n   } else if (n == k) n     res = (n - 1) / 2; n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[100005], p[100005]; n long long cost; n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n; n   for (int i = 0; i < n; ++i) { n     cin >> a[i] >> p[i]; n   } n   int mn = p[0]; n   for (int i = 0; i < n; ++i) mn = min(p[i], mn), cost += mn * 1LL * a[i]; n   cout << cost << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, d, sum = 0, ans = 0; n   cin >> n >> d; n   string *s = new string[d]; n   for (int i = 0; i < d; i++) cin >> s[i]; n   for (int i = 0; i < d; i++) { n     int flag = 0; n     for (int k = 0; k < n; k++) { n       if (s[i][k] ==  0 ) { n         flag = 1; n         break; n       } n     } n     if (flag) n       ans++; n     else n       ans = 0; n     sum = max(sum, ans); n   } n   cout << sum << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MAXN = 1e5 + 5; n const long long mod = 1e9 + 7; n long long fact[MAXN]; n long long invfact[MAXN]; n bool sieve[MAXN]; n long long powe(long long x, long long y, long long mod) { n   long long res = 1; n   while (y > 0) { n     if (y & 1) res = (res * x) % mod; n     y = y >> 1; n     x = (x * x) % mod; n   } n   return res; n } n void makesieve() { n   for (long long i = 0; i < MAXN; ++i) { n     sieve[i] = 1; n   } n   sieve[0] = 0; n   sieve[1] = 0; n   for (long long i = 2; i * i <= MAXN; ++i) { n     for (long long j = 2 * i; j < MAXN; j += i) { n       sieve[j] = 0; n     } n   } n } n void pre() { n   fact[0] = 1; n   invfact[0] = 1; n   for (long long i = 1; i < MAXN; ++i) { n     fact[i] = (fact[i - 1] * i) % mod; n     invfact[i] = powe(fact[i], mod - 2, mod); n   } n   makesieve(); n } n long long comb(long long n, long long r) { n   if (r == 0 || n == r) return 1; n   if (r > n) return 0; n   long long A = (fact[n] * invfact[r]) % mod; n   return (A * invfact[n - r]) % mod; n } n long long gcde(long long a, long long b) { n   if (a == 0) return b; n   return gcde(b % a, a); n } n long long gcdExtended(long long a, long long b, long long *x, long long *y) { n   if (a == 0) { n     *x = 0, *y = 1; n     return b; n   } n   long long x1, y1; n   long long gcd = gcdExtended(b % a, a, &x1, &y1); n   *x = y1 - (b / a) * x1; n   *y = x1; n   return gcd; n } n long long modInverse(long long a, long long m) { n   long long x, y; n   long long g = gcdExtended(a, m, &x, &y); n   return (x % m + m) % m; n } n bool ifcontains(string str, string smlstr) { n   if (str.length() < smlstr.length()) return false; n   for (long long i = 0; i < str.length(); ++i) { n     bool fg = 0; n     for (long long j = 0; j < smlstr.length(); ++j) { n       if (i + j >= str.length()) { n         fg = 1; n         break; n       } n       if (smlstr[j] != str[i + j]) { n         fg = 1; n         break; n       } n     } n     if (!fg) return true; n   } n   return false; n } n int main() { n   pre(); n   long long n, k; n   cin >> n >> k; n   long long ans = 0; n   for (long long i = 0; i <= n; ++i) { n     for (long long j = 0; j <= n; ++j) { n       long long sign; n       if ((i + j) % 2 == 0) n         sign = 1; n       else n         sign = -1; n       long long total_cells = (i + j) * n - i * j; n       long long rest_cells = n * n - total_cells; n       long long temp = n           (powe(k - 1, total_cells, mod) * powe(k, rest_cells, mod)) % mod * n           sign; n       temp = (temp + mod) % mod; n       temp *= (comb(n, i) * comb(n, j)) % mod; n       temp %= mod; n       ans += temp; n       ans %= mod; n     } n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n, m; n   cin >> n >> m; n   int count = 0; n   int a[n + 1][m + 1]; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) cin >> a[i][j]; n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       if (a[1][j] == 1 || a[n][j] == 1) { n         cout <<  2 ; n         count++; n         return; n       } n     } n     if (a[i][1] == 1 || a[i][m] == 1) { n       cout <<  2 ; n       count++; n       return; n     } n   } n   if (count == 0) cout <<  4 ; n } n int main() { n   ios_base::sync_with_stdio(false); n   int t = 1; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char mat[100][100]; n int main(void) { n   int minyX = (1 << 30), minyY = (1 << 30), maxyX = 0, maxyY = 0; n   int n, m; n   scanf( %d %d , &n, &m); n   for (int i = 0; i < n; ++i) { n     scanf( %s , mat[i]); n     for (int j = 0; j < m; ++j) { n       if (mat[i][j] !=  * ) continue; n       if (i < minyX) minyX = i; n       if (i > maxyX) maxyX = i; n       if (j < minyY) minyY = j; n       if (j > maxyY) maxyY = j; n     } n   } n   for (int row = minyX; row <= maxyX; ++row) { n     for (int col = minyY; col <= maxyY; ++col) { n       printf( %c , mat[row][col]); n     } n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 7; n char s[N]; n int m, n; n void sol() { n   int b[26] = {0}; n   scanf( %d %d , &n, &m); n   int a[N] = {0}; n   scanf( %s , s); n   int t; n   for (int i = 0; i < m; i++) { n     scanf( %d , &t); n     a[t - 1]++; n   } n   a[n - 1] = 1; n   for (int i = n - 1; i >= 0; i--) { n     if (i < n - 1) a[i] += a[i + 1]; n     int num = s[i] -  a ; n     b[num] += a[i]; n   } n   for (int i = 0; i < 26; i++) { n     if (i != 25) n       printf( %d  , b[i]); n     else n       printf( %d n , b[i]); n   } n } n int main() { n   int T; n   scanf( %d , &T); n   while (T--) sol(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 201; n const int logn = 20; n struct matrix { n   double a[maxn][maxn]; n   int n; n   bool is_vec; n   matrix() {} n   void init(int n) { n     this->n = n; n     is_vec = 0; n     for (int i = 1; i <= n; i++) { n       for (int j = 1; j <= n; j++) { n         a[i][j] = 0; n       } n     } n   } n   matrix operator*(const matrix &m2) const { n     matrix ans; n     ans.init(n); n     if (is_vec) { n       ans.is_vec = 1; n       for (int j = 1; j <= n; j++) { n         for (int k = 1; k <= n; k++) { n           ans.a[1][j] += a[1][k] * m2.a[j][k]; n         } n       } n       return ans; n     } n     for (int i = 1; i <= n; i++) { n       for (int j = 1; j <= n; j++) { n         for (int k = 1; k <= n; k++) { n           ans.a[i][j] += a[i][k] * m2.a[k][j]; n         } n       } n     } n     return ans; n   } n }; n matrix A[logn]; n int n; n int px[maxn], py[maxn]; n vector<int> p[maxn][maxn]; n int cnt[maxn]; n bool vis[maxn][maxn]; n void init(); n double cal(int t, int m); n bool is_line(int a, int b, int c); n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d%d , &px[i], &py[i]); n   } n   init(); n   int q; n   scanf( %d , &q); n   while (q--) { n     int t, m; n     scanf( %d%d , &t, &m); n     printf( %.7lf n , cal(t, m)); n   } n   return 0; n } n double cal(int t, int m) { n   m--; n   matrix f; n   f.init(n); n   f.is_vec = 1; n   f.a[1][t] = 1; n   for (int i = 0; i < logn; i++) { n     if ((m >> i) & 1) { n       f = f * A[i]; n     } n   } n   double max_ans = 0; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= cnt[i]; j++) { n       double cur_ans = 0; n       for (int u : p[i][j]) { n         cur_ans += f.a[1][u]; n       } n       cur_ans /= p[i][j].size(); n       max_ans = max(max_ans, cur_ans); n     } n   } n   return max_ans; n } n void init() { n   vector<int> cur; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       if (i == j || vis[i][j]) continue; n       cur.clear(); n       cur.push_back(i); n       cur.push_back(j); n       for (int k = 1; k <= n; k++) { n         if (i == k || j == k || !is_line(i, j, k)) continue; n         cur.push_back(k); n       } n       for (int a : cur) { n         vis[i][a] = 1; n       } n       p[i][++cnt[i]] = cur; n     } n   } n   A[0].init(n); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= cnt[i]; j++) { n       for (int a : p[i][j]) { n         A[0].a[i][a] += 1.0 / cnt[i] / p[i][j].size(); n       } n     } n   } n   for (int i = 1; i < logn; i++) { n     A[i] = A[i - 1] * A[i - 1]; n   } n } n bool is_line(int a, int b, int c) { n   int x1 = px[b] - px[a]; n   int x2 = px[c] - px[a]; n   int y1 = py[b] - py[a]; n   int y2 = py[c] - py[a]; n   return (long long)x1 * y2 - (long long)x2 * y1 == 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> a; n int gcd(int a, int b) { n   if (b == 0) { n     return a; n   } n   return gcd(b, a % b); n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int T; n   cin >> T; n   while (T--) { n     int n; n     cin >> n; n     a.clear(); n     a.resize(n + 1); n     bool can = true; n     for (int i = 1; i <= n; i++) { n       cin >> a[i]; n       if (a[i] % (i + 1) == 0) { n         bool updated = false; n         int max = (a[i] < i + 1) ? a[i] : i; n         for (int j = 1; j + 1 <= max; j++) { n           if (a[i] % (j + 1) != 0) { n             updated = true; n             break; n           } n         } n         if (!updated) { n           can = false; n         } n       } n     } n     if (can) { n       cout <<  YES n ; n     } else { n       cout <<  NO n ; n     } n   } n } n


#include <bits/stdc++.h> n #define fi first n #define se second n #define mp make_pair n using namespace std; n  n typedef pair <int, int> pii; n  n struct UF { n  int fa[3010]; n  void clear(int n) { n   for (int i = 1; i <= n; i++) { n    fa[i] = i; n   } n  } n  int find(int x) { n   if (fa[x] == x) return x; n   return fa[x] = find(fa[x]); n  } n  void merge(int x, int y) { n   x = find(x), y = find(y); n   if (x == y) return ; n   fa[y] = x; n  } n }uf; n  n vector <int> ans; n vector <pii> all; n int G[3010][3010], in[3010], cnt[3010], nin[3010]; n int n, m; n  n void dfs(int x) { n  for (int i = 1; i <= n; i++) { n   if (!G[x][i]) continue; n   G[x][i] = G[i][x] = 0; n   dfs(i); n  } n  ans.push_back(x); n } n  n void print() { n  reverse(ans.begin(), ans.end()); n  printf( %d n , ans.size()); n  for (int i = 0; i < ans.size(); i++) { n   printf( %d  , ans[i]); n  } n  printf(  n ); n } n  n int main() { n  scanf( %d%d , &n, &m); n  for (int i = 0; i < m; i++) { n   int x, y; scanf( %d%d , &x, &y); n   G[x][y] = G[y][x] = 1, in[x]++, in[y]++; n   all.push_back(mp(x, y)); n  } n  int tot = 0; n  for (int i = 1; i <= n; i++) { n   tot += in[i] & 1; n  } n  if (tot <= 2) { n   int rt = 1; n   for (int i = 1; i <= n; i++) { n    if (in[i] & 1) { n     rt = i; n     break; n    } n   } n   dfs(rt); n   print(); n   return 0; n  } n  for (int i = 1; i <= n; i++) { n   if (in[i] % 2 == 0) continue; n   for (int j = 1; j <= n; j++) { n    if (G[i][j] || i == j) cnt[j]++; n   } n  } n  for (int i = 1; i <= n; i++) { n   if (cnt[i] < tot - 1) continue; n   for (int j = 1; j <= n; j++) { n    if (i == j) continue; n    if (cnt[i] == tot - 1 && G[i][j]) continue; n    if (in[j] % 2 == 0) continue; n    uf.clear(n); n    for (int i = 1; i <= n; i++) { n     nin[i] = 0; n    } n    for (int k = 0; k < all.size(); k++) { n     int x = all[k].fi, y = all[k].se; n     if ((x == i || y == i) && x + y - i != j && in[x + y - i] & 1) continue; n     uf.merge(x, y); n     nin[x]++, nin[y]++; n    } n    int eq = -1; n    for (int k = 1; k <= n; k++) { n     if (!nin[k]) continue; n     int val = uf.find(k); n     if (eq == -1) eq = val; n     if (eq != val) goto END; n    } n    for (int k = 1; k <= n; k++) { n     if (k == i || k == j) continue; n     if (in[k] & 1) { n      G[i][k] = G[k][i] = 0; n      ans.push_back(i); n      ans.push_back(k); n     } n    } n    ans.push_back(-1); n    dfs(j); n    print(); n    return 0; n    END:; n   } n  } n  printf( 0 n ); n  return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int A = 1e6 + 5; n const int N = 5e4 + 5; n const int M = 5005; n int dp[A], a[N], l[M], r[M], b[N], ans[M]; n int32_t main() { n   for (int i = 1; i < A; i++) dp[i] = dp[i - 1] ^ i; n   int n, m; n   cin >> n >> m; n   for (int i = 1; i <= n; i++) scanf( %d , a + i); n   for (int i = 0; i < m; i++) scanf( %d%d , l + i, r + i); n   for (int i = 1; i <= n; i++) { n     b[i - 1] = 0; n     for (int j = i; j <= n; j++) n       b[j] = max(b[j - 1], a[i] < a[j] ? dp[a[j]] ^ dp[a[i] - 1] n                                        : dp[a[i]] ^ dp[a[j] - 1]); n     for (int j = 0; j < m; j++) n       if (l[j] <= i) ((ans[j]) = max((ans[j]), (b[r[j]]))); n   } n   for (int i = 0; i < m; i++) printf( %d n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int x[n], y[n]; n   for (int i = 0; i < n; i++) cin >> x[i]; n   for (int i = 0; i < n; i++) cin >> y[i]; n   int de = 0, in = 0; n   for (int i = 0; i < n; i++) { n     if (x[i] > y[i]) de = de + x[i] - y[i]; n     if (y[i] > x[i]) in = in + y[i] - x[i]; n   } n   if (de >= in) n     cout <<  Yes ; n   else n     cout <<  No ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long cnt1, cnt2, x, y; n   cin >> cnt1 >> cnt2 >> x >> y; n   long long low = 1, high = 4000000000LL; n   if (x == y) { n     while (low < high) { n       long long mid = low + (high - low) / 2; n       if (mid - mid / x >= cnt1 + cnt2) n         high = mid; n       else n         low = mid + 1; n     } n   } else { n     while (low < high) { n       long long mid = low + (high - low) / 2; n       if (mid - mid / x >= cnt1 && mid - mid / y >= cnt2 && n           mid - mid / (x * y) >= cnt1 + cnt2) n         high = mid; n       else n         low = mid + 1; n     } n   } n   cout << low; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   long long a, b; n   cin >> a >> b; n   int maxa = sqrt(a); n   int maxb = maxa * (maxa + 1); n   if (b >= maxb) n     cout <<  Vladik ; n   else n     cout <<  Valera ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   if (n <= 2) { n     cout << 0 << endl; n   } else { n     n -= 1; n     cout << n / 2 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<pair<int, int>, pair<int, int> > tab; n int main() { n   int n, v[3]; n   pair<int, int> ans; n   int maior = 0; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d%d%d , &v[0], &v[1], &v[2]); n     sort(v, v + 3); n     for (int j = 0; j < 3; j++) n       for (int k = j + 1; k < 3; k++) { n         pair<int, int> x = tab[make_pair(v[j], v[k])]; n         int ind = 3 - j - k; n         int aux[3] = {v[0], v[1], v[2]}; n         aux[ind] += x.first; n         sort(aux, aux + 3); n         if (aux[0] > maior) { n           maior = aux[0]; n           ans.first = i; n           ans.second = x.second; n         } n       } n     if (tab[make_pair(v[0], v[1])].first < v[2]) n       tab[make_pair(v[0], v[1])] = make_pair(v[2], i); n     if (tab[make_pair(v[0], v[2])].first < v[1]) n       tab[make_pair(v[0], v[2])] = make_pair(v[1], i); n     if (tab[make_pair(v[1], v[2])].first < v[0]) n       tab[make_pair(v[1], v[2])] = make_pair(v[0], i); n   } n   if (ans.first > ans.second) swap(ans.first, ans.second); n   if (ans.first) n     printf( 2 n%d %d n , ans.first, ans.second); n   else n     printf( 1 n%d n , ans.second); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 7; n int a[N]; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n, x; n     cin >> n >> x; n     int ok = 0; n     for (int i = 1; i <= n; i++) { n       cin >> a[i]; n       if (a[i] == x) { n         ok = 1; n       } n     } n     if (ok == 1) { n       cout << 1 <<   n ; n       continue; n     } n     sort(a + 1, a + 1 + n); n     int b = a[n]; n     if (x < b) { n       cout << 2 <<   n ; n     } else if (x % b == 0) { n       cout << x / b <<   n ; n     } else { n       cout << x / b + 1 <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long a, b, c, i, j; n   cin >> a >> b >> c; n   long long s = min(a / 3, min(b / 2, c / 2)); n   a -= s * 3; n   b -= s * 2; n   c -= s * 2; n   set<long long> sett; n   for (i = 1; i <= 7; i++) { n     long long cnt = 0; n     long long x = a, y = b, z = c; n     for (j = i; j < (i + 7); j++) { n       if (j % 7 == 1 || j % 7 == 4 || j % 7 == 0) { n         if (!x) n           break; n         else n           x--; n         cnt++; n       } else if (j % 7 == 2 || j % 7 == 6) { n         if (!y) n           break; n         else n           y--; n         cnt++; n       } else if (j % 7 == 3 || j % 7 == 5) { n         if (!z) n           break; n         else n           z--; n         cnt++; n       } n     } n     sett.insert(cnt); n   } n   auto it = sett.end(); n   it--; n   long long ans = *it + s * 7; n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n template <class t> n vector<t> sort(vector<t> &a) { n   return sort(a.begin(), a.end()); n } n const int N = 200200; n int a[N], b[N]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(); n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0; i < m; i++) cin >> b[i]; n   reverse(a, a + n); n   reverse(b, b + m); n   a[n] = -1; n   int pos = 0, mn = a[0]; n   while (pos < n && mn > b[0]) pos++, mn = min(mn, a[pos]); n   if (pos == n || mn < b[0]) { n     puts( 0 ); n     return 0; n   } n   assert(mn == b[0]); n   long long res = 1; n   for (int itr = 0; itr < m - 1; itr++) { n     bool f = 1; n     int npos = pos; n     while (npos < n && mn != b[itr + 1]) { n       npos++; n       mn = min(mn, a[npos]); n       if (f && mn < b[itr]) { n         f = 0; n         res = (res * (npos - pos)) % 998244353; n       } n     } n     if (npos == n || mn != b[itr + 1]) { n       puts( 0 ); n       return 0; n     } n     pos = npos; n   } n   if (*min_element(a + pos, a + n) != b[m - 1]) { n     puts( 0 ); n     return 0; n   } n   cout << res <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n string str[110]; n int rev[110]; n bool counted[110]; n stack<int> st; n queue<int> q; n int length; n bool isRev(string s, string t) { n   for (int i = 0; i < m; i++) n     if (s[i] != t[m - i - 1]) return false; n   return true; n } n bool same(string s) { n   char c = s[0]; n   for (int i = 1; i < m; i++) n     if (c != s[i]) return false; n   return true; n } n bool loneRev(string s) { n   int i = 0, j = m - 1; n   while (i <= j) { n     if (s[i] != s[j]) return false; n     i++; n     j--; n   } n   return true; n } n int main() { n   cin >> n >> m; n   for (int i = 1; i <= n; i++) cin >> str[i]; n   for (int i = 1; i <= n; i++) { n     if (!rev[i]) { n       for (int j = i + 1; j <= n; j++) { n         if (isRev(str[i], str[j])) { n           rev[i] = j; n           rev[j] = i; n           break; n         } n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     if (rev[i] && !counted[i]) { n       counted[i] = true; n       counted[rev[i]] = true; n       q.push(i); n       st.push(rev[i]); n       length += (2 * m); n     } n   } n   for (int i = 1; i <= n; i++) { n     if (!counted[i]) { n       if (same(str[i]) || loneRev(str[i])) { n         length += m; n         q.push(i); n         break; n       } n     } n   } n   while (st.size()) { n     int x = st.top(); n     st.pop(); n     q.push(x); n   } n   cout << length << endl; n   while (q.size()) { n     int x = q.front(); n     q.pop(); n     cout << str[x]; n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<int> > g; n vector<vector<int> > tims; n vector<int> part; n vector<int> conf; n int n; n int e; n int T = 0; n void build_part() { n   bool ok = false; n   while (!ok) { n     for (int(i) = (1); (i) < (T + 1); ++(i)) { n       part[i] = rand() % 2; n     } n     for (int(i) = 0; (i) < (n); ++(i)) { n       ok = false; n       int cnt[] = {0, 0}; n       for (int(j) = 0; (j) < (tims[i].size()); ++(j)) { n         cnt[part[tims[i][j]]] = 1; n         if (cnt[0] * cnt[1] == 1) { n           ok = true; n           break; n         } n       } n       if (!ok) { n         break; n       } n     } n   } n   for (int(i) = (1); (i) < (T + 1); ++(i)) { n     ++part[i]; n   } n } n void build() { n   conf[0] = 1; n   for (int(i) = (1); (i) < (n); ++(i)) { n     int d[] = {0, 0}; n     for (int(j) = 0; (j) < (g[i].size()); ++(j)) { n       if (conf[g[i][j]] > 0) { n         ++d[conf[g[i][j]] - 1]; n       } n     } n     if (d[0] > d[1]) { n       conf[i] = 2; n     } else n       conf[i] = 1; n   } n } n void solve() { n   srand(22812323); n   cin >> n >> e; n   g.resize(n); n   tims.resize(n); n   conf.resize(n, 0); n   for (int(i) = 0; (i) < (e); ++(i)) { n     int a, b; n     cin >> a >> b; n     --a; n     --b; n     g[a].push_back(b); n     g[b].push_back(a); n   } n   for (int(i) = 0; (i) < (n); ++(i)) { n     int l; n     cin >> l; n     tims[i].resize(l); n     for (int(j) = 0; (j) < (tims[i].size()); ++(j)) { n       cin >> tims[i][j]; n       T = max(T, tims[i][j]); n     } n   } n   part.resize(T + 1); n   build_part(); n   build(); n   for (int(i) = 0; (i) < (n); ++(i)) { n     for (int(j) = 0; (j) < (tims[i].size()); ++(j)) { n       if (conf[i] == part[tims[i][j]]) { n         cout << tims[i][j] <<    ; n         break; n       } n     } n   } n   cout << endl; n   for (int(i) = (1); (i) < (T + 1); ++(i)) { n     cout << part[i] <<    ; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n int main(void) { n   int stack[100000 + 1], sp = 0, c; n   stack[0] =   0 ; n   while (c = getchar(), c !=   n ) { n     if (c == stack[sp]) n       --sp; n     else n       stack[++sp] = c; n   } n   printf( %s n , sp == 0 ?  Yes  :  No ); n   exit(EXIT_SUCCESS); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   vector<vector<int> > A(n, vector<int>(m)), B(n, vector<int>(m)); n   for (int x = 0; x < n; x++) n     for (int y = 0; y < m; y++) cin >> A[x][y]; n   for (int x = 0; x < n; x++) n     for (int y = 0; y < m; y++) cin >> B[x][y]; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) n       if (A[i][j] < B[i][j]) swap(A[i][j], B[i][j]); n   } n   bool legal = true; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (i + 1 < n && A[i][j] >= A[i + 1][j]) legal = false; n       if (j + 1 < m && A[i][j] >= A[i][j + 1]) legal = false; n     } n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (i + 1 < n && B[i][j] >= B[i + 1][j]) legal = false; n       if (j + 1 < m && B[i][j] >= B[i][j + 1]) legal = false; n     } n   } n   if (legal) n     cout <<  Possible n ; n   else n     cout <<  Impossible n ; n } n


#include <bits/stdc++.h> n const int maxi = 2000000000; n const int maxq = 1000000000; n const double eps = 1e-10; n const double pi = 3.1415926535897932; n const double inf = 1e+18; n const int mo = 1000000007; n const int maxn = 500000; n using namespace std; n int n; n string s[555]; n vector<char> v; n int le[555][555], color[5555], kol[5555]; n void dfs(char ch) { n   if (color[ch] == 1) { n     puts( Impossible ); n     exit(0); n   } else if (color[ch] == 2) n     return; n   color[ch] = 1; n   for (char i =  a ; i <=  z ; i++) n     if (le[ch][i]) { n       dfs(i); n     } n   v.push_back(ch); n   color[ch] = 2; n } n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> s[i]; n   } n   for (int i = 0; i < n; i++) { n     for (int j = i + 1; j < n; j++) { n       bool ok = false; n       for (int k = 0; k < min(s[i].size(), s[j].size()); k++) n         if (s[i][k] != s[j][k]) { n           le[s[i][k]][s[j][k]] = 1; n           kol[s[j][k]]++; n           ok = true; n           break; n         } n       if (ok == false && s[j].size() < s[i].size()) { n         puts( Impossible ); n         return 0; n       } n     } n   } n   for (char ch =  a ; ch <=  z ; ch++) n     if (color[ch] == 0 && kol[ch] == 0) dfs(ch); n   reverse(v.begin(), v.end()); n   if (v.size() < 26) { n     puts( Impossible ); n     return 0; n   } n   for (int i = 0; i < v.size(); i++) cout << v[i]; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Vec2 { n   int x, y; n   bool left, right, top, bottom; n   Vec2() : x(0), y(0), left(false), right(false), top(false), bottom(false) {} n }; n int main() { n   int n; n   Vec2 points[200]; n   int ans = 0; n   cin >> n; n   for (int i = 0; i < n; ++i) cin >> points[i].x >> points[i].y; n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < n; ++j) { n       if (points[i].x > points[j].x && points[i].y == points[j].y) n         points[j].right = true; n       if (points[i].x < points[j].x && points[i].y == points[j].y) n         points[j].left = true; n       if (points[i].y < points[j].y && points[i].x == points[j].x) n         points[j].bottom = true; n       if (points[i].y > points[j].y && points[i].x == points[j].x) n         points[j].top = true; n     } n   for (int i = 0; i < n; ++i) n     if (points[i].left && points[i].right && points[i].top && points[i].bottom) n       ans += 1; n   cout << ans; n   return 0; 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, x, y; n     cin >> n >> x >> y; n     long long val; n     for (int i = n - 1; i >= 1; i--) { n       if (abs(y - x) % i == 0) { n         val = (y - x) / i; n         break; n       } n     } n     vector<int> v; n     for (int i = x; i <= y; i += val) { n       v.push_back(i); n     } n     if (v.size() == n) { n       for (int i = 0; i < v.size(); i++) cout << v[i] <<    ; n       cout <<   n ; n     } else { n       long long m = n - v.size(); n       while (m--) { n         if ((v[0] - val) > 0) n           v.insert(v.begin(), v[0] - val); n         else n           v.push_back(v[v.size() - 1] + val); n       } n       for (int i = 0; i < v.size(); i++) cout << v[i] <<    ; n       cout <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-9; n int v[1024]; n int vv[1024]; n int N; n int try_it(int last) { n   for (int i = (0); i < (N); i++) vv[i] = v[i]; n   for (int i = (last); i < (N); i++) vv[i] = max(0, vv[i] - 1); n   vv[N] = 0; n   int NN = N; n   while (NN > 0 && vv[NN - 1] == 0) --NN; n   int moves = 0, p = -1; n   while (vv[NN - 1] > 0) { n     moves += 3; n     ++p; n     while (vv[p + 1] >= vv[p]) { n       moves += 2; n       ++p; n     } n     ++moves; n     --vv[p]; n     --p; n     while (p >= 0 && vv[p] == vv[p + 1] + 1) { n       --vv[p]; n       --p; n       moves++; n     } n     moves++; n   } n   return moves + N * 3 - 2 * p - last; n } n void do_it(int last) { n   for (int i = (0); i < (N); i++) vv[i] = v[i]; n   for (int i = (last); i < (N); i++) vv[i] = max(0, vv[i] - 1); n   vv[N] = 0; n   int NN = N; n   while (NN > 0 && vv[NN - 1] == 0) --NN; n   int p = -1; n   while (vv[NN - 1] > 0) { n     cout <<  ARA ; n     ++p; n     while (vv[p + 1] >= vv[p]) { n       cout <<  RA ; n       ++p; n     } n     cout <<  L ; n     --vv[p]; n     --p; n     while (p >= 0 && vv[p] == vv[p + 1] + 1) { n       --vv[p]; n       --p; n       cout <<  L ; n     } n     cout <<  A ; n   } n   while (p++ < N - 1) cout <<  AR ; n   cout <<  A ; n   while (p-- > last) cout <<  L ; n   cout <<  A n ; n } n int main() { n   ios::sync_with_stdio(false); n   cin >> N; n   for (int i = (0); i < (N); i++) cin >> v[i]; n   v[N] = 0; n   while (N > 0 && v[N - 1] == 0) --N; n   int best = try_it(0); n   int last = 0; n   for (int i = (1); i < (N); i++) { n     int c = try_it(i); n     if (c < best) { n       best = c; n       last = i; n     } n   } n   do_it(last); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int t = 1; n   cin >> t; n   while (t--) { n     long long int i, j, k, n, m = 1e10, f = 1; n     cin >> n; n     vector<long long int> a(n), b(n), in, val; n     for (i = 0; i < n; i++) { n       cin >> a[i], b[i] = a[i]; n       m = min(a[i], m); n     } n     sort(b.begin(), b.end()); n     for (i = 0; i < n; i++) n       if (a[i] % m == 0) in.emplace_back(i), val.emplace_back(a[i]); n     sort(val.begin(), val.end()); n     for (i = 0; i < in.size(); i++) a[in[i]] = val[i]; n     for (i = 0; i < n; i++) n       if (a[i] != b[i]) { n         f = 0; n         break; n       } n     if (f) n       cout <<  YES n ; n     else n       cout <<  NO n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1e9; n const int maxn = 5555; n int s[maxn]; n int main(int argc, char const *argv[]) { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     int t; n     cin >> t; n     s[i] = s[i - 1] + t; n   } n   int res = 0; n   for (int i = 1; i <= n; ++i) { n     for (int j = i; j <= n; ++j) { n       int sum = s[j] - s[i - 1]; n       int t = j - i + 1; n       if (sum > t * 100 && t > res) res = t; n     } n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a, b, c[101], d, e, f, g; n int main() { n   cin >> a >> b; n   for (; d < a; d++) { n     cin >> e; n     c[e]++; n     f = max(f, c[e]); n     if (c[e] == 1) { n       g++; n     } n   } n   f = (f % b) ? (f / b + 1) * b : f; n   cout << f * g - a; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct edge { n   long long a, b, w, ind; n }; n const int LEN = 100100; n const long long INF = 100000000000000; n int n, m, s, t, use[LEN], use1[LEN], ans[LEN], timer, tin[LEN], fup[LEN]; n vector<edge> ar[LEN], ar1[LEN], ar2[LEN]; n edge ed[LEN]; n long long dist[LEN], dist1[LEN], min_dist, a, b, l; n set<pair<long long, int> > q; n void deixtra(long long (&dist)[LEN], vector<edge> (&ar)[LEN], int s); n void get_gate(int v, int ind); n int main() { n   scanf( %i %i %i %i , &n, &m, &s, &t); n   s--; n   t--; n   for (int i = 0; i < m; i++) { n     scanf( %I64d %I64d %I64d , &a, &b, &l); n     ed[i].w = l; n     ed[i].ind = i; n     ed[i].a = b - 1; n     ed[i].b = a - 1; n     ar1[b - 1].push_back(ed[i]); n     swap(ed[i].a, ed[i].b); n     ar[a - 1].push_back(ed[i]); n   } n   deixtra(dist, ar, s); n   deixtra(dist1, ar1, t); n   min_dist = dist[t]; n   for (int i = 0; i < m; i++) n     if (dist[ed[i].a] + ed[i].w + dist1[ed[i].b] == min_dist) { n       ar2[ed[i].a].push_back(ed[i]); n       swap(ed[i].a, ed[i].b); n       ar2[ed[i].a].push_back(ed[i]); n       swap(ed[i].a, ed[i].b); n     } n   get_gate(t, -1); n   for (int i = 0; i < m; i++) n     if (ans[i]) n       printf( YES n ); n     else if (dist[ed[i].a] + ed[i].w + dist1[ed[i].b] - min_dist + 1 < ed[i].w) n       printf( CAN %I64d n , n              dist[ed[i].a] + ed[i].w + dist1[ed[i].b] - min_dist + 1); n     else n       printf( NO n ); n   return 0; n } n void deixtra(long long (&dist)[LEN], vector<edge> (&ar)[LEN], int s) { n   int x, to, len; n   pair<long long, int> u; n   for (int i = 0; i < n; i++) dist[i] = INF; n   dist[s] = 0; n   q.insert(make_pair(0, s)); n   while (!q.empty()) { n     x = q.begin()->second; n     q.erase(q.begin()); n     for (int i = 0; i < ar[x].size(); i++) { n       to = ar[x][i].b; n       len = ar[x][i].w; n       if (dist[to] > dist[x] + len) { n         q.erase(make_pair(dist[to], to)); n         dist[to] = dist[x] + len; n         q.insert(make_pair(dist[to], to)); n       } n     } n   } n } n void get_gate(int v, int ind) { n   int u; n   tin[v] = fup[v] = timer++; n   use1[v] = 1; n   for (int i = 0; i < ar2[v].size(); i++) { n     if (ar2[v][i].ind == ind) continue; n     u = ar2[v][i].b; n     if (use1[u]) n       fup[v] = min(fup[v], tin[u]); n     else { n       get_gate(u, ar2[v][i].ind); n       fup[v] = min(fup[v], fup[u]); n       if (fup[u] > tin[v]) ans[ar2[v][i].ind] = 1; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1e9; n const long long INF = 1e18; n const int md = 1e9 + 7; n const int N = 2e5 + 1; n vector<int> g[N]; n int p[N], s[N], mx[N], mn[N]; n bool did[N]; n void make(int v) { p[v] = v, s[v] = 1, mx[v] = v, mn[v] = v; } n int fin(int v) { n   if (p[v] == v) return v; n   return p[v] = fin(p[v]); n } n void un(int a, int b) { n   a = fin(a); n   b = fin(b); n   if (a == b) return; n   if (s[a] < s[b]) swap(a, b); n   s[a] += s[b], mx[a] = max(mx[a], mx[b]); n   p[b] = a; n   mn[a] = min(mn[a], mn[b]); n } n signed main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int n, m; n   cin >> n >> m; n   for (int f = 0; f < n; ++f) make(f); n   for (int j = 0; j < m; ++j) { n     int u, v; n     cin >> u >> v; n     --u, --v; n     un(u, v); n   } n   int curr = 0; n   int ans = 0; n   int may; n   bool isn = 0; n   while (curr < n) { n     if (mx[fin(curr)] - mn[fin(curr)] + 1 == s[fin(curr)]) { n       int lead = fin(curr); n       while (curr < n && fin(curr) == lead) { n         curr++; n       } n       isn = 1; n     } else { n       int lead = fin(curr); n       if (isn) may = curr; n       isn = 0; n       while (may < n && fin(may) == lead) { n         may++; n       } n       un(may, curr); n       ans++; n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, nxt[402][26]; n string s, t; n int dp[401][401]; n bool solve2(string t1, string t2) { n   dp[0][0] = -1; n   for (int i = 0; i <= t1.size(); ++i) { n     for (int j = 0; j <= t2.size(); ++j) { n       if (!i && !j) continue; n       dp[i][j] = n; n       if (i) dp[i][j] = min(dp[i][j], nxt[dp[i - 1][j] + 1][t1[i - 1] -  a ]); n       if (j) dp[i][j] = min(dp[i][j], nxt[dp[i][j - 1] + 1][t2[j - 1] -  a ]); n     } n   } n   return dp[t1.size()][t2.size()] < n; n } n void solve() { n   cin >> s >> t; n   n = s.size(); n   m = t.size(); n   for (int i = 0; i < 26; ++i) nxt[n][i] = nxt[n + 1][i] = n; n   for (int i = n - 1; ~i; --i) { n     memcpy(nxt[i], nxt[i + 1], sizeof(nxt[0])); n     nxt[i][s[i] -  a ] = i; n   } n   for (int i = 0; i < m; ++i) n     if (solve2(t.substr(0, i), t.substr(i))) { n       cout <<  YES n ; n       return; n     } n   cout <<  NO n ; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int t; n   cin >> t; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 1000000000ll; n const long long inf64 = inf * inf; n const long long base = inf + 7; n const double pi = acos(-1.0); n long long Abs(long long x) { return (x >= 0 ? x : -x); } n int n; n string s; n int par[62 * 62], size[62 * 62], in[62 * 62], out[62 * 62]; n vector<int> ans, g[62 * 62]; n vector<bool> used; n int _get(char c) { n   if (c >=  0  && c <=  9 ) n     return (c -  0 ); n   else if (c >=  a  && c <=  z ) n     return (c -  a  + 10); n   else n     return (c -  A  + 36); n } n int get(string a) { n   int x, y; n   x = _get(a[0]); n   y = _get(a[1]); n   return x * 62 + y; n } n int find(int x) { return (par[x] == x ? x : par[x] = find(par[x])); } n void un(int a, int b) { n   int pa = find(a); n   int pb = find(b); n   if (size[pa] == size[pb]) n     par[pa] = pb, size[pb]++; n   else { n     if (size[pa] > size[pb]) swap(pa, pb); n     par[pa] = pb; n   } n } n char chr(int x) { n   if (x < 10) n     return char(x +  0 ); n   else if (x < 36) n     return char(x +  a  - 10); n   else n     return char(x +  A  - 36); n } n void dfs(int v) { n   while (!g[v].empty()) { n     int x = g[v].back(); n     g[v].pop_back(); n     dfs(x); n   } n   ans.push_back(v); n } n bool solve() { n   for (int i(0); i < 62 * 62; i++) size[i] = 1, par[i] = i; n   cin >> n; n   used.assign(62 * 62, 0); n   int Q = -1; n   for (int i(0); i < n; i++) { n     int x, y; n     cin >> s; n     x = get(s.substr(0, 2)); n     if (Q == -1) Q = x; n     y = get(s.substr(1, 2)); n     used[x] = used[y] = 1; n     g[x].push_back(y); n     in[y]++; n     out[x]++; n     un(x, y); n   } n   bool f = 0; n   for (int i(0); i < 62 * 62; i++) { n     if (!used[i]) continue; n     if (par[i] != i) continue; n     if (f) { n       puts( NO ); n       return true; n       ; n     } else n       f = 1; n   } n   int x1, x2; n   x1 = x2 = -1; n   for (int i(0); i < 62 * 62; i++) { n     if (in[i] == out[i]) continue; n     if (in[i] - out[i] == 1) { n       if (x1 != -1) { n         puts( NO ); n         return true; n         ; n       } n       x1 = i; n     } else if (in[i] - out[i] == -1) { n       if (x2 != -1) { n         puts( NO ); n         return true; n         ; n       } n       x2 = i; n     } else { n       puts( NO ); n       return true; n       ; n     } n   } n   if (x1 == -1 && x2 != -1 || x1 != -1 && x2 == -1) { n     puts( NO ); n     return true; n     ; n   } n   used.assign(62 * 62, 0); n   dfs(x2 != -1 ? x2 : Q); n   reverse((ans).begin(), (ans).end()); n   puts( YES ); n   int x, y; n   x = ans[0] / 62; n   cout << chr(x); n   for (int i(0); i < (int)ans.size(); i++) { n     x = ans[i] % 62; n     cout << chr(x); n   } n   return true; n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   char alfabetGainuse[] = n        -abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPRQSTUVWXYZ0123456789 ; n   int T; n   cin >> T; n   for (int t = 1; t <= T; t++) { n     int r, c, k; n     cin >> r >> c >> k; n     char harta[r + 1][c + 1]; n     cin.get(); n     for (int i = 1; i <= r; i++) { n       cin.get(harta[i], c + 1); n       cin.get(); n     } n     int mancare = 0; n     for (int i = 1; i <= r; i++) { n       for (int j = 0; j < c; j++) { n         if (harta[i][j] ==  R ) mancare++; n       } n     } n     int mancareGainuse[k + 1]; n     for (int i = 1; i <= k; i++) { n       mancareGainuse[i] = mancare / k; n     } n     for (int i = 1; i <= mancare % k; i++) { n       mancareGainuse[i]++; n     } n     char hartaNoua[r + 1][c + 2]; n     int indexGaina = 1; n     int mancareGasita = 0; n     for (int i = 1; i <= r; i++) { n       hartaNoua[i][c] =   0 ; n       if (i % 2 == 1) { n         for (int j = 0; j < c; j++) { n           if (indexGaina == k + 1) { n             hartaNoua[i][j] = alfabetGainuse[indexGaina - 1]; n           } else { n             hartaNoua[i][j] = alfabetGainuse[indexGaina]; n             if (harta[i][j] ==  R ) { n               mancareGasita++; n               if (mancareGasita == mancareGainuse[indexGaina]) { n                 indexGaina++; n                 mancareGasita = 0; n               } n             } n           } n         } n       } else { n         for (int j = c - 1; j >= 0; j--) { n           if (indexGaina == k + 1) { n             hartaNoua[i][j] = alfabetGainuse[indexGaina - 1]; n           } else { n             hartaNoua[i][j] = alfabetGainuse[indexGaina]; n             if (harta[i][j] ==  R ) { n               mancareGasita++; n               if (mancareGasita == mancareGainuse[indexGaina]) { n                 indexGaina++; n                 mancareGasita = 0; n               } n             } n           } n         } n       } n     } n     for (int i = 1; i <= r; i++) { n       cout << hartaNoua[i] <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> temp(3001, 0); n vector<vector<long long> > dp(3001, temp); n void solve() { n   long long n; n   cin >> n; n   vector<long long> v(n); n   long long i, j, k, l; n   for (i = 0; i < n; i++) cin >> v[i]; n   for (i = 0; i < 3001; i++) { n     for (j = 0; j < 3001; j++) dp[i][j] = 0; n   } n   for (i = 0; i < n; i++) { n     dp[v[i]][i] = dp[v[i]][i] + 1; n     if (i == 0) continue; n     for (j = 0; j < 3001; j++) dp[j][i] += dp[j][i - 1]; n   } n   long long ans = 0; n   for (i = 0; i < n; i++) { n     for (j = i + 1; j < n; j++) { n       long long a = v[i]; n       long long b = v[j]; n       if (a == b) continue; n       if (dp[b][i] > 0 && dp[a][j] < dp[a][n - 1]) { n         long long val1 = dp[b][i]; n         long long val2 = dp[a][n - 1] - dp[a][j]; n         ans = ans + val1 * val2; n       } n     } n   } n   for (i = 0; i <= 3000; i++) { n     if (dp[i][n - 1] >= 4) { n       long long val = 1; n       long long count = dp[i][n - 1]; n       val = val * count; n       count--; n       val = val * count; n       val = val / 2LL; n       count--; n       val = val * count; n       val = val / 3LL; n       count--; n       val = val * count; n       val = val / 4LL; n       ans = ans + val; n     } n   } n   cout << ans << endl; n } n int main() { n   int t = 1; n   cin >> t; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k, max = 0, count = 0; n   cin >> n >> k; n   vector<int> v(n); n   for (int i = 0; i < n; i++) { n     cin >> v[i]; n     if (v[i - 1] != v[i]) n       count++; n     else if (count > max) { n       max = count; n       count = 1; n     } else n       count = 1; n   } n   if (max < count) max = count; n   cout << max; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mx = 1e5 + 1; n bool vis[mx]; n vector<int> adj[mx]; n int n; n bool star() { n   bool ans; n   int count1 = 0; n   int other = 0; n   for (int i = 1; i <= n; i++) { n     if (adj[i].size() == 1) { n       count1++; n     } else if (adj[i].size() == n - 1) { n       other++; n     } n   } n   if (other == 1 && count1 == n - 1) { n     ans = true; n   } else { n     ans = false; n   } n   return ans; n } n bool ring() { n   bool ans = true; n   for (int i = 1; i <= n; i++) { n     if (adj[i].size() != 2) { n       ans = false; n       break; n     } n   } n   return ans; n } n bool bus() { n   bool ans; n   int one = 0; n   int two = 0; n   for (int i = 1; i <= n; i++) { n     if (adj[i].size() == 1) { n       one++; n     } else if (adj[i].size() == 2) { n       two++; n     } n   } n   if (one == 2 && two == n - 2) { n     ans = true; n   } else { n     ans = false; n   } n   return ans; n } n int main() { n   int m, k, u, v; n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     cin >> u >> v; n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   if (star()) { n     cout <<  star topology  << endl; n   } else if (bus()) { n     cout <<  bus topology  << endl; n   } else if (ring()) { n     cout <<  ring topology  << endl; n   } else { n     cout <<  unknown topology  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long a[2000005]; n long long sum[2000005]; n int main() { n   long long m, n, i, j, k, ans = 0; n   cin >> m; n   for (i = 1; i <= m; i++) scanf( %lld , &a[i]); n   sort(a + 1, a + 1 + m); n   for (i = m; i >= 1; i--) sum[i] = sum[i + 1] + a[i]; n   for (i = 1; i <= m; i *= 4) { n     ans += sum[m - i + 1]; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9 + 1; n const int N = 5003; n vector<int> adj[N]; n vector<pair<int, int> > stores[N]; n bool done[N]; n int dist[N]; n void clear(int n) { n   for (int i = 1; i <= n; ++i) done[i] = 0; n } n void modify(multiset<pair<int, int> >& tiendas, const int& to, n             long long& rpta) { n   for (pair<int, int> p : stores[to]) { n     auto it = tiendas.rbegin(); n     pair<int, int> q = *it; n     int left = p.second; n     while (!tiendas.empty() && q.first > p.first) { n       if (q.second >= left) { n         tiendas.erase(tiendas.find(q)); n         rpta -= ((long long)q.first * q.second); n         if (q.second != left) { n           tiendas.insert({q.first, q.second - left}); n           rpta += ((long long)q.first * (q.second - left)); n         } n         left = 0; n         break; n       } else { n         tiendas.erase(tiendas.find(q)); n         left -= q.second; n         rpta -= ((long long)q.first * q.second); n       } n       if (!tiendas.empty()) { n         it = tiendas.rbegin(); n         q = *it; n       } n     } n     if (p.second != left) { n       tiendas.insert({p.first, p.second - left}); n       rpta += ((long long)p.first * (p.second - left)); n     } n   } n   return; n } n int bfs(int source, int prod, long long price) { n   multiset<pair<int, int> > tiendas; n   tiendas.insert({INF, prod}); n   long long rpta = (long long)INF * prod; n   queue<int> Q; n   Q.push(source); n   dist[source] = 0; n   done[source] = 1; n   modify(tiendas, source, rpta); n   if (rpta <= price) return dist[source]; n   while (!Q.empty()) { n     int from = Q.front(); n     Q.pop(); n     for (int to : adj[from]) { n       if (done[to]) continue; n       dist[to] = dist[from] + 1; n       done[to] = 1; n       Q.push(to); n       modify(tiendas, to, rpta); n       if (rpta <= price) return dist[to]; n     } n   } n   return -1; n } n void solve() { n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < m; ++i) { n     int u, v; n     cin >> u >> v; n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   int w; n   cin >> w; n   for (int i = 0; i < w; ++i) { n     int c, k, p; n     cin >> c >> k >> p; n     stores[c].push_back({p, k}); n   } n   int q; n   cin >> q; n   for (int i = 0; i < q; ++i) { n     clear(n); n     int g, r, a; n     cin >> g >> r >> a; n     cout << bfs(g, r, a) <<   n ; n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(nullptr); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 220; n bool vis[maxn]; n vector<int> G[maxn]; n int ans; n int dfs(int v, int x) { n   int sum = 0; n   int max1 = 0, max2 = 0; n   for (int i = 0; i < G[v].size(); i++) { n     if (G[v][i] != x) { n       sum = max(sum, dfs(G[v][i], v)); n       if (ans > max1) { n         max2 = max1; n         max1 = ans; n       } else n         max2 = ans > max2 ? ans : max2; n     } n   } n   sum = max(sum, max1 + max2); n   ans = max1 + 1; n   return sum; n } n int main() { n   int n, a, b; n   while (scanf( %d , &n) != EOF) { n     for (int i = 0; i <= n; i++) G[i].clear(); n     for (int i = 0; i < n - 1; i++) { n       scanf( %d%d , &a, &b); n       G[a].push_back(b); n       G[b].push_back(a); n     } n     int last_ans = 0; n     for (int i = 0; i < n; i++) { n       int num = G[i].size(), temp; n       for (int j = 0; j < num; j++) { n         int ans = 0; n         temp = dfs(i, G[i][j]) * dfs(G[i][j], i); n         last_ans = max(last_ans, temp); n       } n     } n     printf( %d n , last_ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int d1, d2, d3, i; n   cin >> d1 >> d2 >> d3; n   int k[4] = {(d1 + d2 + d3), (2 * d1 + 2 * d2), (2 * d2 + 2 * d3), n               (2 * d1 + 2 * d3)}; n   int min = 1e9; n   for (i = 0; i < 4; i++) { n     if (k[i] < min) min = k[i]; n   } n   cout << min; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n T in() { n   char ch; n   T n = 0; n   bool ng = false; n   while (1) { n     ch = getchar(); n     if (ch ==  - ) { n       ng = true; n       ch = getchar(); n       break; n     } n     if (ch >=  0  && ch <=  9 ) break; n   } n   while (1) { n     n = n * 10 + (ch -  0 ); n     ch = getchar(); n     if (ch <  0  || ch >  9 ) break; n   } n   return (ng ? -n : n); n } n template <typename T> n inline T Dis(T x1, T y1, T x2, T y2) { n   return sqrt((x1 - x2 * x1 - x2) + (y1 - y2 * y1 - y2)); n } n template <typename T> n inline T POW(T B, T P) { n   if (P == 0) return 1; n   if (P & 1) n     return B * POW(B, P - 1); n   else n     return (POW(B, P / 2) * POW(B, P / 2)); n } n template <typename T> n inline T ABS(T a) { n   if (a < 0) n     return -a; n   else n     return a; n } n template <typename T> n inline T Gcd(T a, T b) { n   if (a < 0) return Gcd(-a, b); n   if (b < 0) return Gcd(a, -b); n   return (b == 0) ? a : Gcd(b, a % b); n } n template <typename T> n inline T Lcm(T a, T b) { n   if (a < 0) return Lcm(-a, b); n   if (b < 0) return Lcm(a, -b); n   return a * (b / Gcd(a, b)); n } n long long Bigmod(long long base, long long power, long long MOD) { n   long long ret = 1; n   while (power) { n     if (power & 1) ret = (ret * base) % MOD; n     base = (base * base) % MOD; n     power >>= 1; n   } n   return ret; n } n long long ModInverse(long long number, long long MOD) { n   return Bigmod(number, MOD - 2, MOD); n } n int Set(int N, int pos) { return N = N | (1 << pos); } n int Reset(int N, int pos) { return N = N & ~(1 << pos); } n bool Chkbit(int N, int pos) { return (bool)(N & (1 << pos)); } n bool isVowel(char ch) { n   ch = toupper(ch); n   if (ch ==  A  || ch ==  U  || ch ==  I  || ch ==  O  || ch ==  E ) n     return true; n   return false; n } n bool isConst(char ch) { n   if (isalpha(ch) && !isVowel(ch)) return true; n   return false; n } n int toInt(string s) { n   int sm; n   stringstream ss(s); n   ss >> sm; n   return sm; n } n long long int toLlint(string s) { n   long long int sm; n   stringstream ss(s); n   ss >> sm; n   return sm; n } n const int N = int(2e6) + 15; n int cmp(int *r, int a, int b, int l) { n   return (r[a] == r[b]) && (r[a + l] == r[b + l]); n } n int wa[N], wb[N], wws[N], wv[N]; n int rnk[N], height[N]; n void DA(int *r, int *sa, int n, int m) { n   int i, j, p, *x = wa, *y = wb, *t; n   for (i = 0; i < m; i++) wws[i] = 0; n   for (i = 0; i < n; i++) wws[x[i] = r[i]]++; n   for (i = 1; i < m; i++) wws[i] += wws[i - 1]; n   for (i = n - 1; i >= 0; i--) sa[--wws[x[i]]] = i; n   for (j = 1, p = 1; p < n; j *= 2, m = p) { n     for (p = 0, i = n - j; i < n; i++) y[p++] = i; n     for (i = 0; i < n; i++) n       if (sa[i] >= j) y[p++] = sa[i] - j; n     for (i = 0; i < n; i++) wv[i] = x[y[i]]; n     for (i = 0; i < m; i++) wws[i] = 0; n     for (i = 0; i < n; i++) wws[wv[i]]++; n     for (i = 1; i < m; i++) wws[i] += wws[i - 1]; n     for (i = n - 1; i >= 0; i--) sa[--wws[wv[i]]] = y[i]; n     for (t = x, x = y, y = t, p = 1, x[sa[0]] = 0, i = 1; i < n; i++) n       x[sa[i]] = cmp(y, sa[i - 1], sa[i], j) ? p - 1 : p++; n   } n } n void calheight(int *r, int *sa, int n) { n   int i, j, k = 0; n   for (i = 1; i <= n; i++) rnk[sa[i]] = i; n   for (i = 0; i < n; height[rnk[i++]] = k) n     for (k ? k-- : 0, j = sa[rnk[i] - 1]; r[i + k] == r[j + k]; k++) n       ; n } n int sa[N], data[N], n; n void Deal() { n   DA(data, sa, n + 1, 128); n   calheight(data, sa, n); n } n char s[2000002]; n int Len; n struct z { n   int opn, cls; n }; n z Tree[2000002 * 4]; n void Update(int id, int l, int r, int pos) { n   if (l == r) { n     if (s[pos] ==  ( ) { n       Tree[id].opn = 1; n     } else n       Tree[id].cls = 1; n     return; n   } n   int mid = (l + r) / 2; n   int lft = 2 * id; n   int rgt = lft + 1; n   if (pos <= mid) { n     Update(lft, l, mid, pos); n   } else { n     Update(rgt, mid + 1, r, pos); n   } n   int mn = min(Tree[lft].opn, Tree[rgt].cls); n   Tree[id].cls = Tree[lft].cls + Tree[rgt].cls - mn; n   Tree[id].opn = Tree[rgt].opn + Tree[lft].opn - mn; n   return; n } n z Query(int id, int l, int r, int st, int ed) { n   if (st <= l && ed >= r) { n     return Tree[id]; n   } n   int mid = (l + r) / 2; n   int lft = 2 * id; n   int rgt = lft + 1; n   if (ed <= mid) { n     return Query(lft, l, mid, st, ed); n   } else if (st > mid) { n     return Query(rgt, mid + 1, r, st, ed); n   } else { n     z a, b, c; n     a = Query(lft, l, mid, st, ed); n     b = Query(rgt, mid + 1, r, st, ed); n     int mn = min(a.opn, b.cls); n     c.cls = a.cls + b.cls - mn; n     c.opn = b.opn + a.opn - mn; n     return c; n   } n } n int dp[2000002], Close[2000002], len, dp1[2000002]; n void Solve() { n   int Last = -1, Last1 = -1; n   for (int i = Len + 1 - 1; i >= 1; i--) { n     if (s[i] ==  ) ) n       Last = -1; n     else if (Last == -1) { n       Last = i; n     } n     dp[i] = (Last == -1 ? 0 : (Last - i + 1)); n     if (s[i] ==  ( ) n       Last1 = -1; n     else if (Last1 == -1) { n       Last1 = i; n     } n     dp1[i] = (Last1 == -1 ? 0 : (Last1 - i + 1)); n   } n } n int A[4 * 2000002]; n void Update(int id, int l, int r, int pos, int val) { n   if (l == r) { n     A[id] = height[pos]; n     return; n   } n   int mid = (l + r) / 2; n   int lft = 2 * id; n   int rgt = lft + 1; n   if (pos <= mid) { n     Update(lft, l, mid, pos, val); n   } else { n     Update(rgt, mid + 1, r, pos, val); n   } n   A[id] = min(A[lft], A[rgt]); n   return; n } n int Lcp(int id, int l, int r, int st, int ed) { n   if (st <= l && ed >= r) { n     return A[id]; n   } n   int mid = (l + r) / 2; n   int lft = 2 * id; n   int rgt = lft + 1; n   if (ed <= mid) { n     return Lcp(lft, l, mid, st, ed); n   } else if (st > mid) { n     return Lcp(rgt, mid + 1, r, st, ed); n   } else { n     int a, b; n     a = Lcp(lft, l, mid, st, ed); n     b = Lcp(rgt, mid + 1, r, st, ed); n     return min(a, b); n   } n } n int HandleJhamela(int id, int id1, int Opn, int mx) { n   int Suru = id + min(len, dp[id]), Suru1 = id1 + min(len, dp[id1]); n   int Rnk = rnk[Suru - 1], Rnk1 = rnk[Suru1 - 1]; n   int L1 = len + mx - Opn - Close[id], L2 = len + mx - Opn - Close[id1]; n   int f = 0; n   if (Rnk > Rnk1) swap(Rnk, Rnk1), f = 1; n   int lcp = Lcp(1, 1, n, Rnk + 1, Rnk1); n   if (f) swap(Rnk, Rnk1); n   if (lcp >= min(L1, L2)) { n     int diff = abs(L2 - L1); n     if (L1 < L2) { n       if (dp1[id1 + min(len, dp[id1]) + lcp] < diff) n         return id1; n       else n         return id; n     } else { n       if (dp1[id + min(len, dp[id]) + lcp] < diff) n         return id; n       else n         return id1; n     } n   } else { n     if (Rnk < Rnk1) n       return id; n     else n       return id1; n   } n } n int main() { n   n = 0; n   scanf( %s , &s); n   len = strlen(s); n   for (int i = len + 1 - 1; i >= 1; i--) s[i] = s[i - 1]; n   Len = len * 2; n   for (int i = len + 1; i < Len + 1; i++) s[i] = s[i - len]; n   Solve(); n   for (int i = 1; i < Len + 1; i++) { n     Update(1, 1, Len, i); n     data[n] = (int)s[i]; n     n++; n   } n   data[n] = 0; n   Deal(); n   for (int i = 1; i < n; i++) Update(1, 1, n, i, height[i]); n   int mx = (1 << 28), Open = 0, Id = 0; n   for (int i = 1; i < len + 1; i++) { n     z ans = Query(1, 1, Len, i, (i + len - 1)); n     int Cst = ans.cls + ans.opn; n     Close[i] = ans.opn; n     int CntOpn = ans.cls + (min(len, dp[i])); n     if (mx > Cst) { n       mx = Cst; n       Open = CntOpn; n       Id = i; n     } else if (mx == Cst && CntOpn > Open) { n       Open = CntOpn; n       Id = i; n     } else if (mx == Cst && CntOpn == Open) { n       Id = HandleJhamela(Id, i, CntOpn, mx); n     } n   } n   for (int i = 0; i < Open; i++) { n     printf( ( ); n   } n   int baki = len + mx - Open - Close[Id]; n   int St = Id + dp[Id]; n   for (int i = 0; i < baki; i++) { n     printf( %c , s[St + i]); n   } n   for (int i = 0; i < Close[Id]; i++) { n     printf( ) ); n   } n   printf(  n ); n   return 0; n } n