malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e4 + 10;   const int maxk = 2e4 + 10;   const int inf = 0x3f3f3f3f;   int n, m, k;   bool g[maxn][maxn];   struct Block {     int l, r, b, t;   };   Block b[maxk];   int bnum;   bool vis[maxk];   int d[maxk];   int res;   int start, stop;   void bfs() {     memset(d, 0, sizeof d);     res = inf;     queue<int> q;     q.push(start);     while (q.size()) {       int u = q.front();       q.pop();       vis[u] = true;       if (u == stop) {         res = min(d[u], res);         continue;       } else if (b[u].b >= n - 1 || b[u].r >= m - 1) {         res = min(d[u] + 1, res);         continue;       }       for (int i = 0; i < bnum; i++) {         if (vis[i]) continue;         if (b[u].t > b[i].b + 2 && (b[u].l > b[i].r + 2 || b[u].r < b[i].l - 2))           continue;         if (b[u].b < b[i].t - 2 && (b[u].l > b[i].r + 2 || b[u].r < b[i].l - 2))           continue;         vis[i] = true;         d[i] = d[u] + 1;         q.push(i);       }     }   }   bool dvis[maxn][maxn];   void dfs(int r, int c, int bnum) {     if (dvis[r][c]) return;     if (r == 1 && c == 1) start = bnum;     if (r == n && c == m) stop = bnum;     dvis[r][c] = 1;     b[bnum].l = min(b[bnum].l, c);     b[bnum].r = max(b[bnum].r, c);     b[bnum].b = max(b[bnum].b, r);     b[bnum].t = min(b[bnum].t, r);     if (r > 1 && g[r - 1][c]) dfs(r - 1, c, bnum);     if (r < n && g[r + 1][c]) dfs(r + 1, c, bnum);     if (c > 1 && g[r][c - 1]) dfs(r, c - 1, bnum);     if (c < m && g[r][c + 1]) dfs(r, c + 1, bnum);   }   int main() {     memset(g, 0, sizeof g);     scanf( %d%d%d , &n, &m, &k);     for (int i = 0; i < k; i++) {       int r, c;       scanf( %d%d , &r, &c);       g[r][c] = 1;     }     bnum = 0;     start = stop = -1;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         if (g[i][j] && !dvis[i][j]) {           b[bnum].r = b[bnum].l = j;           b[bnum].b = b[bnum].t = i;           dfs(i, j, bnum);           bnum++;         }       }     }     bfs();     if (res < inf)       printf( %d n , res);     else       printf( -1 n );   }
#include <bits/stdc++.h>   using namespace std;   int a, s, d[10004][2], f, g, h, j, k, l, i, n, m;   string x, z;   set<string> q;   bool chk(int idx, int dis) {     for (int i = idx; i < idx + dis; i++) {       if (x[i] != x[i + dis]) return true;     }     return false;   }   int main() {     cin >> x;     n = x.size();     if (n >= 7) {       d[n - 2][0] = 1;       z = z + x[n - 2] + x[n - 1];       q.insert(z);     }     if (n >= 8) {       d[n - 3][1] = 1;       z = x[n - 3] + z;       q.insert(z);     }     for (i = n - 4; i >= 5; i--) {       d[i][0] = d[i + 2][1];       if (chk(i, 2)) d[i][0] |= d[i + 2][0];       if (d[i][0]) {         z =   ;         z = z + x[i] + x[i + 1];         q.insert(z);       }       d[i][1] = d[i + 3][0];       if (chk(i, 3)) d[i][1] |= d[i + 3][1];       if (d[i][1]) {         z =   ;         z = z + x[i] + x[i + 1] + x[i + 2];         q.insert(z);       }     }     cout << q.size() << endl;     for (set<string>::iterator it = q.begin(); it != q.end(); it++) {       cout << *it << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   bool IsPrime(int x) {     for (int i = 2; i * i <= x; i++)       if (x % i == 0) return false;     return true;   }   int modpow(int w, int p, int m) {     if (p == 0) return 1;     int x = modpow(w, p / 2, m);     x = 1LL * x * x % m;     if (p & 1) x = 1LL * x * w % m;     return x;   }   int main() {     int L, x, p;     scanf( %d%d , &L, &x);     p = L + 1;     if ((x == 2) || !IsPrime(p)) {       puts( -1 );       return 0;     }     if (L == 1) {       printf( %d n , x - 1);       return 0;     }     for (int b = x - 1; b > 1; b--) {       if (modpow(b, L, p) != 1) continue;       if (b % p == 0) continue;       long long t = 0, r = 1, n = 0, x, d;       while (1) {         t = t + 1;         x = r * b;         d = x / p;         r = x % p;         n = n * b + d;         if (r == 1) break;         if (t > p / 2) {           printf( %d n , b);           return 0;         }       }       if (t == L) {         printf( %d n , b);         return 0;       }     }     puts( -1 );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long n, a[100009], m = 0;     cin >> n;     for (long i = long(0); i < long(n); i++) {       cin >> a[i];       m = max(m, a[i]);     }     long c = 0, q = 0;     for (long i = long(0); i < long(n); i++) {       if (a[i] == m)         c++;       else {         q = max(q, c);         c = 0;       }     }     q = max(q, c);     cout << q <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   struct dat {     int L, R, V;   } a[100005];   int N, M;   bool cmp(dat a, dat b) {     if (a.L != b.L) return a.L < b.L;     if (a.R != b.R) return a.R < b.R;     return a.V < b.V;   }   int ans[100005], s[100005];   void build(int k) {     int last = 0;     for (int i = (1), _b = (M); i <= _b; i++)       if (a[i].V >> k & 1) {         for (int j = (max(last + 1, a[i].L)), _b = (a[i].R); j <= _b; j++)           ans[j] |= (1 << k);         last = max(last, a[i].R);       }     memset(s, 0, sizeof s);     for (int i = (1), _b = (N); i <= _b; i++)       s[i] = s[i - 1] + (!(ans[i] >> k & 1));     for (int i = (1), _b = (M); i <= _b; i++)       if (!(a[i].V >> k & 1))         if (s[a[i].R] == s[a[i].L - 1]) {           cout <<  NO n ;           exit(0);         }   }   int main(void) {     cin >> N >> M;     for (int i = (1), _b = (M); i <= _b; i++) cin >> a[i].L >> a[i].R >> a[i].V;     sort(a + 1, a + M + 1, cmp);     for (int i = (0), _b = (29); i <= _b; i++) build(i);     cout <<  YES n ;     for (int i = (1), _b = (N); i <= _b; i++) cout << ans[i] <<    ;     cout << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     long long int a, b;     cin >> a >> b;     long long int sumo = 0, sume = 0;     for (int i = 1;; i++) {       sumo = i * i;       if (sumo > a) {         cout <<  Vladik  << endl;         break;       }       sume = i * (i + 1);       if (sume > b) {         cout <<  Valera  << endl;         break;       }     }   }
#include <bits/stdc++.h>   using namespace std;   const int N = 155;   int n;   int a[N], b[N], A, B;   int main() {     scanf( %d , &n);     a[0] = 1;     for (int i = 1; i <= n; i++) {       for (int j = 0; j <= A; j++) b[j + 1] = (b[j + 1] + a[j]) % 2;       B = max(B, A + 1);       swap(A, B);       for (int j = 0; j <= A; j++) swap(a[j], b[j]);     }     printf( %d n , A);     for (int i = 0; i <= A; i++) printf( %d%c , a[i], i == A ?   n  :    );     printf( %d n , B);     for (int i = 0; i <= B; i++) printf( %d%c , b[i], i == B ?   n  :    );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int inf = 0x3f3f3f3f;   const int N = 1E6 + 7;   int n;   int id[N], x[N], y[N];   bool cmp(int a, int b) {     if (x[a] < x[b]) return true;     if (x[a] > x[b]) return false;     if (x[a] % 2 == 1)       return y[a] < y[b];     else       return y[a] > y[b];   }   int main() {     scanf( %d , &n);     for (int i = 0; i < n; i++) {       scanf( %d %d , &x[i], &y[i]);       x[i] /= 1000;       y[i] /= 1000;       id[i] = i;     }     sort(id, id + n, cmp);     for (int i = 0; i < n; i++) {       printf( %d  , id[i] + 1);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 500002;   long long a[maxn];   int n, k, d;   void enter() {     cin >> n >> k >> d;     for (int i = (1), _b = (n + 1); i < _b; ++i) cin >> a[i];   }   int seg[4 * maxn];   void update(int l, int r, int st, int q) {     if (l == r)       seg[st] = 1;     else {       int mid = (l + r) >> 1;       if (mid >= q)         update(l, mid, st * 2, q);       else         update(mid + 1, r, st * 2 + 1, q);       seg[st] = max(seg[st * 2], seg[st * 2 + 1]);     }   }   int _get(int l, int r, int u, int v, int st) {     if (u <= l && r <= v)       return seg[st];     else if (l > v || r < u)       return 0;     else {       int mid = (l + r) >> 1;       return max(_get(l, mid, u, v, st * 2), _get(mid + 1, r, u, v, st * 2 + 1));     }   }   void solve() {     sort(a + 1, a + n + 1);     update(0, n, 1, 0);     for (int i = (1), _b = (n + 1); i < _b; ++i) {       int pos = lower_bound(a + 1, a + n + 1, a[i] - d) - a;       if (pos <= i - k + 1) {         if (_get(0, n, pos - 1, i - k, 1) == 1) {           if (i == n) {             cout <<  YES ;             exit(0);           } else             update(0, n, 1, i);         }       }     }     cout <<  NO ;   }   int main() {     enter();     solve();   }
#include <bits/stdc++.h>   using namespace std;   char s[200005];   char x[26];   int n, m;   char a, b;   int main() {     while (scanf( %d%d , &n, &m) != EOF) {       for (int i = 0; i < 26; i++) x[i] = i +  a ;       getchar();       scanf( %s , s);       getchar();       for (int i = 0; i < m; i++) {         scanf( %c , &a);         getchar();         scanf( %c , &b);         getchar();         if (a == b) continue;         for (int j = 0; j < 26; j++) {           if (x[j] == a) {             x[j] = b;             continue;           }           if (x[j] == b) {             x[j] = a;             continue;           }         }       }       int k;       for (int i = 0; i < n; i++) {         k = s[i] -  a ;         printf( %c , x[k]);       }       printf(  n );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     string ch, t;     int n, s, i;     bool ok1 = 0, ok2 = 0;     scanf( %d %d , &n, &s);     if (9 * n >= s) {       if (n == 1 && s == 0) {         cout << 0 <<     << 0 << endl;       } else if (s == 0) {         cout << -1 <<     << -1 << endl;       } else {         while (n--) {           ch += min(9, s) +  0 ;           t = ch;           s -= 9;           if (s < 0) s = 0;         }         reverse(t.begin(), t.end());         if (t[0] ==  0 ) {           t[0] =  1 ;           for (i = 1; i < t.size(); i++) {             if (t[i] >  0 ) {               t[i] = t[i] - 1;               break;             }           }         }         cout << t <<     << ch << endl;       }     } else {       cout << -1 <<     << -1 << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long M = 1000000007;   long long n, k;   long long a[100050];   long long cnt[100050];   map<long long, long long> dict;   long long f[100050];   long long fac[100000 + 50], ifac[100000 + 50];   long long pwk[100050];   long long ksm(long long a, long long x = M - 2) {     long long ret = 1;     for (; x; x >>= 1, a = a * a % M)       if (x & 1) ret = ret * a % M;     return ret;   }   long long C(long long n, long long m) {     if (m < 0 || m > n) return 0;     return fac[n] * ifac[m] % M * ifac[n - m] % M;   }   void prepare() {     fac[0] = 1;     for (int i = 1; i <= 100000; i++) fac[i] = fac[i - 1] * i % M;     ifac[100000] = ksm(fac[100000]);     for (int i = 100000 - 1; i >= 0; i--) ifac[i] = ifac[i + 1] * (i + 1) % M;   }   int main() {     ios_base::sync_with_stdio(false);     prepare();     cin >> n >> k;     for (int i = 1; i <= n; i++) {       cin >> a[i];       dict[a[i]]++;     }     cnt[0] = k;     for (auto y : dict)       if (y.first != -1) cnt[y.second]++, cnt[0]--;     int r = 0;     if (dict.find(-1) != dict.end()) r = dict[-1];     for (int i = 1; i < n; i++) {       f[i + 1] = ksm(M + i - n) *                  ((k - 1) * i % M * f[i - 1] % M +                   ((M + 2 - k) * i % M - n + M) * f[i] % M + i * k % M) %                  M;     }     long long invpwkr = ksm(ksm(k, r));     pwk[0] = 1;     for (int i = 1; i <= n; i++) pwk[i] = pwk[i - 1] * (k - 1) % M;     long long ans = (M - (f[0] * (k - 1) + f[n]) % M) % M;     for (int i = 0; i <= n; i++)       if (cnt[i] != 0) {         for (int j = 0; j <= r; j++)           ans = (ans +                  C(r, j) * f[i + j] % M * cnt[i] % M * invpwkr % M * pwk[r - j]) %                 M;       }     cout << ans <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   deque<char> D;   int main() {     int n;     cin >> n;     string s;     cin >> s;     for (int i = 0; i < n; i++)       if (D.size() % 2 == 0 || D.back() != s[i]) D.push_back(s[i]);     if (D.size() % 2 == 1) D.pop_back();     cout << n - D.size() << endl;     for (int i = 0; i < D.size(); i++) cout << D[i];     cout << endl;     return 0;   }
#include <bits/stdc++.h>   int const MAX = 1e5 + 5;   int a[MAX], need[MAX];   bool has[MAX], pos[MAX];   int main() {     int n, num = 0;     scanf( %d , &n);     memset(has, false, sizeof(has));     memset(pos, false, sizeof(pos));     for (int i = 0; i < n; i++) {       scanf( %d , &a[i]);       if (!has[a[i]] && a[i] <= n)         has[a[i]] = true;       else         pos[i] = true;     }     int cnt = 0;     for (int i = 1; i <= n; i++)       if (!has[i]) need[cnt++] = i;     int idx = 0;     for (int i = 0; i < n; i++)       if (pos[i]) a[i] = need[idx++];     for (int i = 0; i < n - 1; i++) printf( %d  , a[i]);     printf( %d n , a[n - 1]);   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 100003;   pair<int, int> t[4 * maxn];   int psh[maxn];   void update(int v, int l, int r, int q) {     if (l == r) return void(t[v] = psh[q] ? make_pair(1, 0) : make_pair(0, 1));     int m = (l + r) / 2;     if (q <= m) update(v * 2, l, m, q);     if (q > m) update(v * 2 + 1, m + 1, r, q);     int u = min(t[v * 2].first, t[v * 2 + 1].second);     t[v] = make_pair(t[v * 2].first + t[v * 2 + 1].first - u,                      t[v * 2].second + t[v * 2 + 1].second - u);   }   int get_ans(int v, int l, int r, int c) {     if (!t[v].first) return -1;     if (l == r) return psh[l];     int m = (l + r) / 2;     if (t[v * 2 + 1].first - c > 0)       return get_ans(v * 2 + 1, m + 1, r, c);     else       return get_ans(v * 2, l, m, c - t[v * 2 + 1].first + t[v * 2 + 1].second);   }   int main() {     int n;     scanf( %d , &n);     for (int i = 0; i < n; ++i) {       int q, t, num;       scanf( %d %d , &q, &t);       if (t == 1) {         scanf( %d , &num);         psh[q] = num;       }       update(1, 1, n, q);       printf( %d n , get_ans(1, 1, n, 0));     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n;   vector<int> tree[3005];   int p[3005][3005];   long long sz[3005][3005];   long long dfs(int root, int cur) {     sz[root][cur] = 1;     for (int nxt : tree[cur]) {       if (!sz[root][nxt]) {         sz[root][cur] += dfs(root, nxt);         p[root][nxt] = cur;       }     }     return sz[root][cur];   }   long long dp[3005][3005];   long long f(int a, int b) {     long long& ret = dp[a][b];     if (~ret) return ret;     ret = 0;     if (a == b) return ret;     return ret = sz[b][a] * sz[a][b] + max(f(a, p[a][b]), f(p[b][a], b));   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     memset(dp, -1, sizeof(dp));     cin >> n;     for (int i = 1; i < n; i++) {       int a, b;       cin >> a >> b;       tree[a].push_back(b);       tree[b].push_back(a);     }     for (int i = 1; i <= n; i++) dfs(i, i);     long long ans = 0;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++) ans = max(ans, f(i, j));     }     cout << ans;   }
#include <bits/stdc++.h>   using namespace std;   int d1, d2, d3;   int r;   int main() {     cin >> d1 >> d2 >> d3;     r = d1 + d2 + d3;     r = min(r, d1 + d1 + d2 + d2);     r = min(r, d1 + d3 + d1 + d3);     r = min(r, d2 + d3 + d2 + d3);     cout << r <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       long long int n, m;       cin >> n >> m;       long long int s = 0, x, y, i, j;       vector<long long int> a, b;       for (i = 0; i < n; i++) {         cin >> x;         if (x < 0)           a.push_back(x);         else           b.push_back(x);       }       sort(a.begin(), a.end());       sort(b.begin(), b.end());       if (a.size() > 0 && b.size() > 0) {         if (abs(a[0]) > b[b.size() - 1]) {           s += abs(a[0]);           for (i = m; i < a.size(); i += m) s += abs(2 * a[i]);           for (i = b.size() - 1; i >= 0; i -= m) s += (2 * b[i]);         } else {           s += b[b.size() - 1];           for (i = b.size() - 1 - m; i >= 0; i -= m) s += (2 * b[i]);           for (i = 0; i < a.size(); i += m) s += abs(2 * a[i]);         }       } else if (a.size() > 0) {         s += abs(a[0]);         for (i = m; i < a.size(); i += m) s += abs(2 * a[i]);       } else {         s += b[b.size() - 1];         for (i = b.size() - 1 - m; i >= 0; i -= m) s += (2 * b[i]);       }       cout << s << endl;     }     return 0;   }
#include <bits/stdc++.h>   int main() {     int i, j, count, n, a[5001], flag[5001];     scanf( %d , &n);     count = 0;     memset(flag, 0, sizeof(int) * 5001);     for (i = 0; i < n; i++) {       scanf( %d , &a[i]);       if (a[i] > n)         count++;       else if (flag[a[i]] == 1)         count++;       else         flag[a[i]] = 1;     }     printf( %d n , count);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     ;     int n;     cin >> n;     vector<int> v(n);     int mx = 0;     for (int i = 0; i < n; i++) {       cin >> v[i];       mx = max(mx, v[i]);     }     int count = 1, l = 0;     for (int i = 0; i < n; i++) {       if (i && v[i] == v[i - 1] && v[i] == mx)         count++;       else {         l = max(count, l);         count = 1;       }       if (i == n - 1) l = max(count, l);     }     cout << l << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   multiset<int> a, b;   int c[300010], d[300010];   int main() {     int n;     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> c[i] >> d[i];       a.insert(c[i]);       b.insert(d[i]);     }     int ans = 0;     for (int i = 1; i <= n; i++) {       a.erase(a.find(c[i]));       b.erase(b.find(d[i]));       ans = max(ans, *b.begin() - *a.rbegin());       a.insert(c[i]);       b.insert(d[i]);     }     cout << ans << endl;   }
#include <bits/stdc++.h>   using namespace std;   int n, k, sx, ex;   char s[1010];   int main() {     scanf( %d%d%s , &n, &k, s);     for (int i = 0; i < n; i++) {       if (s[i] ==  G ) sx = i;       if (s[i] ==  T ) ex = i;     }     if (sx > ex) swap(sx, ex);     if ((ex - sx) % k != 0)       printf( NO n );     else {       bool f = 1;       for (int i = sx + k; i < ex; i += k) {         if (s[i] ==  # ) f = 0;       }       if (f)         printf( YES n );       else         printf( NO n );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   signed main() {     ios ::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long t;     cin >> t;     while (t--) {       long long n, k;       cin >> n >> k;       string s;       cin >> s;       vector<long long> cnt(26);       for (auto u : s) ++cnt[u -  a ];       long long ans = 0;       for (long long period = 1; period <= n; ++period) {         if (k % period) continue;         for (long long times = 1; times <= n; ++times) {           long long length = 0;           for (long long c = 0; c < 26; ++c) length += cnt[c] / times;           if (length < period) continue;           ans = max(ans, period * times);         }       }       cout << ans <<   n ;     }   }
#include <bits/stdc++.h>   using namespace std;   inline long long read() {     long long s = 0, w = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) w = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) s = s * 10 + ch -  0 , ch = getchar();     return s * w;   }   void put1() { puts( Yes ); }   void put2() { puts( No ); }   void put3() { puts( -1 ); }   long long qp(long long a, long long b, long long p) {     long long ans = 1;     while (b) {       if (b & 1) {         ans = (ans * a) % p;         --b;       }       a = (a * a) % p;       b >>= 1;     }     return ans % p;   }   const unsigned long long base = 2333;   const unsigned long long pp = 19260811;   const unsigned long long ppp = 999998639;   const int manx = 2e5 + 5;   const int mo = 998244353;   map<long long, long long> dp;   long long a[manx];   vector<long long> q;   int main() {     long long n = read();     long long ma = 0, ans = 0;     for (int i = 1; i <= n; i++) {       a[i] = read();       dp[a[i]] = max(dp[a[i] - 1] + 1, dp[a[i]]);       if (dp[a[i]] > ans) ans = dp[a[i]], ma = a[i];     }     for (int i = n; i >= 1; i--)       if (a[i] == ma) q.push_back(i), ma--;     reverse(q.begin(), q.end());     cout << q.size() << endl;     for (auto x : q) cout << x <<    ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   void solve() {     long long a, b, c;     cin >> a >> b;     c = a + b;     string s = to_string(a);     string x = to_string(b);     string y = to_string(c);     string e =   , f =   , g =   ;     for (int i = 0; i < s.size(); i++) {       if (s[i] !=  0 ) {         e += s[i];       }     }     for (int i = 0; i < x.size(); i++) {       if (x[i] !=  0 ) {         f += x[i];       }     }     for (int i = 0; i < y.size(); i++) {       if (y[i] !=  0 ) {         g += y[i];       }     }     long long m, n, p;     m = stoi(e);     n = stoi(f);     p = stoi(g);     if (m + n == p)       cout <<  YES n ;     else       cout <<  NO n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int t;     t = 1;     while (t--) {       solve();     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   set<int> Tmp, Trans;   int dp[(500 + 5)][(500 + 5)], n, m, A[(500 + 5)], B[(500 + 5)];   int from[(500 + 5)][(500 + 5)][2], rkA[(500 + 5)], rkB[(500 + 5)];   int trans[(500 + 5)][(500 + 5)];   int getrk(int x) {     int TP = 0;     for (set<int>::iterator i = Trans.begin(); i != Trans.end(); ++i) {       ++TP;       if (*i == x) return TP;     }     return -1;   }   void output(int x, int y) {     if (y == 0 || x == 0) return;     output(from[x][y][0], from[x][y][1]);     cout << B[y] <<    ;   }   int main() {     cin >> n;     for (int i = 1; i <= n; ++i) {       cin >> A[i];       Tmp.insert(A[i]);     }     cin >> m;     for (int i = 1; i <= m; ++i) {       cin >> B[i];       if (Tmp.find(B[i]) != Tmp.end()) Trans.insert(B[i]);     }     for (int i = 1; i <= n; ++i) {       if (Trans.find(A[i]) != Trans.end())         rkA[i] = getrk(A[i]);       else         rkA[i] = 0;     }     for (int i = 1; i <= m; ++i) {       if (Trans.find(B[i]) != Trans.end())         rkB[i] = getrk(B[i]);       else         rkB[i] = 0;     }     for (int i = 1; i <= n; ++i) {       for (int j = 1; j <= (int)Trans.size(); ++j) {         trans[i][j] = max(trans[i - 1][j], (rkA[i] == j) * i);       }     }     for (int x = 1; x <= n; ++x) {       for (int i = 1; i <= m; ++i) {         if (A[x] != B[i]) continue;         for (int k = 0; k < i; ++k) {           if (rkB[k] >= rkB[i]) continue;           if (dp[trans[x - 1][rkB[k]]][k] + 1 > dp[x][i]) {             dp[x][i] = dp[trans[x - 1][rkB[k]]][k] + 1;             from[x][i][0] = trans[x - 1][rkB[k]];             from[x][i][1] = k;           }         }       }     }     int Ans = 0, x, y, z;     for (int i = 1; i <= n; ++i) {       for (int j = 1; j <= m; ++j) {         if (A[i] == B[j] && dp[i][j] > Ans) {           Ans = dp[i][j];           x = i, y = j;         }       }     }     cout << Ans << endl;     if (Ans != 0) output(x, y);   }