Songs

Time limit1sMemory limit128 MB

Summary
Given songs with lengths and play frequencies, sort them by length-to-frequency ratio (stable on ties) to minimize expected access time and report the song at a queried position.
Level

Medium4 of 10

Topics
Greedy, Sorting
Solved
No attempts yet

Problem

John Doe is a famous DJ and therefore faces the problem of optimizing how songs are placed on his tapes. For a given tape, and for each song on that tape, John knows the length of the song and how often it is played. His task is to record the songs on the tape in an order that minimizes the expected access time.

If the songs are recorded in the order Ss(1),…,Ss(n)S_{s(1)}, \dots, S_{s(n)}, then the quantity to minimize is

∑i=1nfs(i)∑j=1ils(j)\sum_{i=1}^{n} f_{s(i)} \sum_{j=1}^{i} l_{s(j)}

where fs(i)f_{s(i)} is the playing frequency of the song placed ii-th and ls(j)l_{s(j)} is the length of the song placed jj-th. (The inner sum is the time needed to reach the ii-th song: the total length of the songs recorded up to and including it.) Can you help John?

Input

The input is read from a text file and consists of several data sets. A data set starts with the number NN (which fits in a 16-bit integer) of songs. Then follow the NN song specifications and, finally, a number giving the position of a song SS on the optimized tape. A song specification consists of the song identifier (an integer), the length of the song (which fits in a 16-bit integer), and the playing frequency of the song (a floating-point number). White space may occur freely in the input, which is always valid and terminates at end of file.

Output

For each data set, print on its own line, starting at the beginning of the line, the identifier of the song that ends up at the requested position in an order minimizing the expected access time. If two songs have the same length-to-frequency ratio l/fl/f, swapping them leaves the cost unchanged; in that case they are kept in the order they appear in the input, so the song at each position is uniquely determined.

Examples1

  1. Example 1

    Input
    5
    1 10 45.5
    2 5 20
    30 20 10
    400  50 35
    15 17 89.9
    3
    
    Expected output
    2