import std; int readInt() { int x; readf!" %d"(x); return x; } void main() { int n = readInt; auto a = new int[](n).map!(i => readInt); int[][int] hashMap; foreach(i; 0 .. n) { int x = readInt; hashMap[x] ~= i; } auto add = new int[][](n + 1); auto remove = new int[][](n + 1); foreach(key, value; hashMap) { int min = value[0]; int max = value[$ - 1]; add[min] ~= key; remove[max + 1] ~= key; } auto rbtree = new RedBlackTree!int; auto b = new int[](n); foreach(i; 0 .. n) { foreach(j; add[i]) rbtree.insert(j); foreach(j; remove[i]) rbtree.removeKey(j); b[i] = rbtree.back; } writeln(b.map!(i => i.to!string).join(" ")); }