Booklets
Time limit1sMemory limit128 MB
Given booklets sorted by page count split among schools using floor/ceiling division with a strict serving order, find the first booklet's page count given to a specified school.
- Level
Medium4 of 10
- Topics
- Sorting, Math, Implementation
- Solved
- No attempts yet
Problem
Bob must distribute advertising booklets for extra-curricular activities to several schools. The booklets have different numbers of pages. Given the page count of every booklet and the number of schools, Bob hands them out so that each school receives either the lower integer part (LIP) or the upper integer part (UIP) of (number of booklets) / (number of schools) — that is, or booklets, where is the number of booklets and is the number of schools.
Bob must also obey these rules:
- He serves all the schools that receive the UIP count of booklets first, and only then the schools that receive the LIP count.
- If a school is served before a school (that is, ), then every booklet given to has no more pages than any booklet given to . Equivalently, across schools the booklets are handed out in non-decreasing order of pages.
- The booklets given to a single school are handed out in the same relative order in which they appear on Bob's list.
Back at the company, Bob's boss asks for the number of pages of the first booklet distributed to a particular school, where schools are numbered from in the order Bob served them. Compute that number.
Input
The input contains several data sets; read them until the end of the file. Each data set gives, in order: the number of schools, the index of the school specified by the boss (numbered from ), the number of booklets (fewer than ), and then the number of pages of each booklet (each value fits in an integer). Whitespace may appear freely between the numbers. The input is always valid.
Output
For each data set, print on its own line the number of pages of the first booklet distributed to the specified school.