Coding Contest
Time limit1sMemory limit256 MB
Place coders in the free starting slots so the queue contest that pairs the strongest and weakest of each front triple leaves the strongest possible survivor.
- Level
Medium7 of 10
- Topics
- Binary search, Simulation, Greedy
- Solved
- No attempts yet
Problem
The club is holding a coding contest for Sujin's birthday.
N coders numbered 1 through N enter the contest, and coder has a skill value . N is odd.
The contest is played in teams of two, so the N + 1 people, Sujin included, split into pairs. The organizers keep the teams balanced by forming them this way.
- Line up the N contestants in a single row.
- Repeat the following until one contestant is left in the row.
- Look at the skills of the three contestants at the front of the row.
- Among the three, take the one with the highest skill. If several of them share the highest skill, take the one with the smallest number.
- Among the three, take the one with the lowest skill. If several of them share the lowest skill, take the one with the largest number.
- The two contestants taken above form a team and leave the row.
- Send the remaining contestant to the back of the row.
- The one contestant left at the end teams up with Sujin.
The coders numbered M or below already have a fixed starting position in the row. Sujin can place the remaining N - M coders in the empty positions in any order she likes, and she wants to team up with a coder whose skill is as high as possible. Write a program that computes the largest skill a coder teamed up with Sujin can have.
Input
The first line contains the number of coders N and the number of coders whose starting position is fixed, M.
Each of the next M lines contains the coding skill and the starting position of coder ().
Each of the next N - M lines contains the coding skill of coder (), one per line.
Output
Print on the first line the largest coding skill a coder teamed up with Sujin can have.
Constraints
- , and N is odd.
- ()
- ()
- when ()