RMID - Running Median


You will be given some integers in non decreasing order and each time the median is queried you have to report and remove it. Take the smaller element as median in case of even elements.

Input

The input contains many test cases. Read until End Of File.

Each test case contains n (n ≤ 100000) positive integers in non-decreasing order, along with m queries indicated by -1, all on separate lines. (See the example.)

For a query, print the current median on a single line and remove it from the list. 

Each test case ends with 0 on a single line, and two test cases will be separated by an empty line. All integers are guaranteed to fit in a signed 32-bit container. A query can only occur if the list is non-empty. 

Output

For each test case output m lines containing the answers to the corresponding queries. Print an empty line after each test case. 

Example

Input:
1
2
3
4
-1
-1
5
6
7
-1
0

2
3
-1
0

Output:
2
3
5

2

hide comments
Anuj Arora: 2016-08-28 12:17:51

Phew...a few minor optimizations....and accepted....nice list question

naruto09: 2016-03-31 16:49:46

Ok unexpected AC...!!

DEVVRAT GUPTA: 2015-12-25 05:29:11

50th AC

MAYANK NARULA: 2015-12-15 12:30:55

why does it TLE??.........
I used a doubly linked list and maintained a head node for start,end,mid pointers..also storing count..

satish9040: 2015-10-17 10:38:34

use printf,scanf :) Happy coding :)

benzema009: 2015-09-22 20:20:27

time constraint is too harsh!

benzema009: 2015-09-18 11:44:06

getting TLE... No java submissions till now in this problem

Last edit: 2015-09-18 11:54:48
:.Mohib.:: 2015-05-25 05:35:02

AC in one go.... easy :)

Sunil: 2015-04-13 19:40:33

can there be duplicates??

Akhilesh Anandh: 2013-11-28 11:51:09

Also try RMID2

Last edit: 2015-01-29 14:13:02

Added by:jayant mukherji
Date:2013-07-12
Time limit:0.5s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM32-GCC MAWK BC C-CLANG NCSHARP CPP14 CPP14-CLANG COBOL COFFEE D-CLANG D-DMD DART ELIXIR FANTOM FORTH GOSU GRV JS-MONKEY JULIA KTLN NIM OBJC OBJC-CLANG OCT PICO PROLOG PYPY PYPY3 R RACKET RUST CHICKEN SQLITE SWIFT UNLAMBDA VB.NET