import java.io.BufferedReader ;
import java.io.InputStreamReader ;
import java.util.Map ;
import java.util.Scanner ;
import java.util.TreeMap ;
public class Main {
Scanner sc
= new Scanner
( System .
in ) ; int m = sc.nextInt ( ) ;
int n = sc.nextInt ( ) ;
int [ ] nums = new int [ m] ;
for ( int i = 0 ; i < m; i++ ) {
nums[ i] = sc.nextInt ( ) ;
}
TreeMap
< Integer , Integer
> blackBox
= new TreeMap
<> ( ) ; blackBox.
put ( Integer .
MAX_VALUE ,
1 ) ; int size = 1 ;
int curValue = 1 ;
int curPosition = 0 ;
for ( int i = 0 ; i < n; i++ ) {
int req = sc.nextInt ( ) ;
for ( int j = size - 1 ; j < req; j++ ) {
if ( blackBox.containsKey ( nums[ j] ) ) {
blackBox.put ( nums[ j] , blackBox.get ( nums[ j] ) + 1 ) ;
if ( curKey == nums[ j] ) {
curValue++;
}
}
else {
blackBox.put ( nums[ j] , 1 ) ;
}
if ( nums[ j] < curKey) {
if ( curPosition == 0 ) {
Map .
Entry < Integer , Integer
> curEnrty
= blackBox.
lowerEntry ( curKey
) ; curKey = curEnrty.getKey ( ) ;
curValue = curEnrty.getValue ( ) ;
curPosition = curValue - 1 ;
}
else {
curPosition--;
}
}
}
size = req + 1 ;
if ( curPosition == curValue - 1 ) {
Map .
Entry < Integer , Integer
> curEnrty
= blackBox.
higherEntry ( curKey
) ; curKey = curEnrty.getKey ( ) ;
curValue = curEnrty.getValue ( ) ;
curPosition = 0 ;
}
else {
curPosition++;
}
}
}
}
aW1wb3J0IGphdmEuaW8uQnVmZmVyZWRSZWFkZXI7CmltcG9ydCBqYXZhLmlvLklucHV0U3RyZWFtUmVhZGVyOwppbXBvcnQgamF2YS51dGlsLk1hcDsKaW1wb3J0IGphdmEudXRpbC5TY2FubmVyOwppbXBvcnQgamF2YS51dGlsLlRyZWVNYXA7CgpwdWJsaWMgY2xhc3MgTWFpbiB7CiAgICBwdWJsaWMgc3RhdGljIHZvaWQgbWFpbihTdHJpbmdbXSBhcmdzKSB0aHJvd3MgRXhjZXB0aW9uewogICAgICAgIFNjYW5uZXIgc2MgPSBuZXcgU2Nhbm5lcihTeXN0ZW0uaW4pOwogICAgICAgIGludCBtID0gc2MubmV4dEludCgpOwogICAgICAgIGludCBuID0gc2MubmV4dEludCgpOwogICAgICAgIGludFtdIG51bXMgPSBuZXcgaW50W21dOwogICAgICAgIGZvcihpbnQgaSA9IDA7IGkgPCBtOyBpKyspewogICAgICAgICAgICBudW1zW2ldID0gc2MubmV4dEludCgpOwogICAgICAgIH0KICAgICAgICBUcmVlTWFwPEludGVnZXIsIEludGVnZXI+IGJsYWNrQm94ID0gbmV3IFRyZWVNYXA8PigpOwogICAgICAgIGJsYWNrQm94LnB1dChJbnRlZ2VyLk1BWF9WQUxVRSwgMSk7CiAgICAgICAgaW50IHNpemUgPSAxOwogICAgICAgIGludCBjdXJLZXkgPSBJbnRlZ2VyLk1BWF9WQUxVRTsKICAgICAgICBpbnQgY3VyVmFsdWUgPSAxOwogICAgICAgIGludCBjdXJQb3NpdGlvbiA9IDA7CiAgICAgICAgZm9yKGludCBpID0gMDsgaSA8IG47IGkrKyl7CiAgICAgICAgICAgIGludCByZXEgPSBzYy5uZXh0SW50KCk7CiAgICAgICAgICAgIGZvcihpbnQgaiA9IHNpemUgLSAxOyBqIDwgcmVxOyBqKyspewogICAgICAgICAgICAgICAgaWYgKGJsYWNrQm94LmNvbnRhaW5zS2V5KG51bXNbal0pKSB7CiAgICAgICAgICAgICAgICAgICAgYmxhY2tCb3gucHV0KG51bXNbal0sIGJsYWNrQm94LmdldChudW1zW2pdKSArIDEpOwogICAgICAgICAgICAgICAgICAgIGlmIChjdXJLZXkgPT0gbnVtc1tqXSkgewogICAgICAgICAgICAgICAgICAgICAgICBjdXJWYWx1ZSsrOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGVsc2UgewogICAgICAgICAgICAgICAgICAgIGJsYWNrQm94LnB1dChudW1zW2pdLCAxKTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGlmKG51bXNbal0gPCBjdXJLZXkpIHsKICAgICAgICAgICAgICAgICAgICBpZiAoY3VyUG9zaXRpb24gPT0gMCkgewogICAgICAgICAgICAgICAgICAgICAgICBNYXAuRW50cnk8SW50ZWdlciwgSW50ZWdlcj4gY3VyRW5ydHk9IGJsYWNrQm94Lmxvd2VyRW50cnkoY3VyS2V5KTsKICAgICAgICAgICAgICAgICAgICAgICAgY3VyS2V5ID0gY3VyRW5ydHkuZ2V0S2V5KCk7CiAgICAgICAgICAgICAgICAgICAgICAgIGN1clZhbHVlID0gY3VyRW5ydHkuZ2V0VmFsdWUoKTsKICAgICAgICAgICAgICAgICAgICAgICAgY3VyUG9zaXRpb24gPSBjdXJWYWx1ZSAtIDE7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIGVsc2UgewogICAgICAgICAgICAgICAgICAgICAgICBjdXJQb3NpdGlvbi0tOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgICBzaXplID0gcmVxICsgMTsKICAgICAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKGN1cktleSk7CiAgICAgICAgICAgIGlmIChjdXJQb3NpdGlvbiA9PSBjdXJWYWx1ZSAtIDEpIHsKICAgICAgICAgICAgICAgICBNYXAuRW50cnk8SW50ZWdlciwgSW50ZWdlcj4gY3VyRW5ydHkgPSBibGFja0JveC5oaWdoZXJFbnRyeShjdXJLZXkpOwogICAgICAgICAgICAgICAgY3VyS2V5ID0gY3VyRW5ydHkuZ2V0S2V5KCk7CiAgICAgICAgICAgICAgICBjdXJWYWx1ZSA9IGN1ckVucnR5LmdldFZhbHVlKCk7CiAgICAgICAgICAgICAgICBjdXJQb3NpdGlvbiA9IDA7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgZWxzZSB7CiAgICAgICAgICAgICAgICBjdXJQb3NpdGlvbisrOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQp9