import Data.List
child [] n = []
child (x:xs) n = child xs n ++ (if 2*x+1<=n then [2*x,2*x+1] else if 2*x<=n then [2*x] else [])
get [] p = []
get (x:xs) p = (get xs p) ++ [p!!(x-1)]
solve [] p n = []
solve xs p n
= [maximum (get xs p
)] ++ solve
(child xs n
) p n
main = do
x <- readInts
y <- readInts
aW1wb3J0IENvbnRyb2wuTW9uYWQKaW1wb3J0IFN5c3RlbS5JTwppbXBvcnQgRGF0YS5MaXN0CmNoaWxkIFtdIG4gPSBbXQpjaGlsZCAoeDp4cykgbiA9IGNoaWxkIHhzIG4gKysgKGlmIDIqeCsxPD1uIHRoZW4gWzIqeCwyKngrMV0gZWxzZSBpZiAyKng8PW4gdGhlbiBbMip4XSBlbHNlIFtdKQoKCmdldCBbXSBwID0gW10KZ2V0ICh4OnhzKSBwID0gKGdldCB4cyBwKSArKyBbcCEhKHgtMSldCgpzb2x2ZSBbXSBwIG4gPSBbXQpzb2x2ZSB4cyBwIG4gPSBbbWF4aW11bSAoZ2V0IHhzIHApXSArKyBzb2x2ZSAoY2hpbGQgeHMgbikgcCBuCgpyZWFkSW50cyA6OiBJTyBbSW50XQpyZWFkSW50cyA9IGZtYXAgKG1hcCByZWFkLndvcmRzKSBnZXRMaW5lCgptYWluIDo6IElPICgpCm1haW4gPSBkbwoJeCA8LSByZWFkSW50cwoJeSA8LSByZWFkSW50cwoJcHV0U3RyIChpbnRlcmNhbGF0ZSAiICIgKG1hcCBzaG93IChzb2x2ZSBbMV0geSAoeCEhMCkpKSk=