#K2981. Find K Largest Values

    ID: 24857 Type: Default 1000ms 256MiB

Find K Largest Values

Find K Largest Values

You are given a dataset of integers and an integer \( k \). Your task is to determine the \( k \) largest values from the dataset. The answer should be computed using a min-heap technique. More formally, given a list \( A = [a_1, a_2, \dots, a_n] \), your program must output the \( k \) largest elements in descending order.

The input consists of several datasets. For each dataset:

  • The first line contains an integer \( k \), representing the number of largest values to find.
  • The second line contains an integer \( n \), representing the number of elements in the dataset.
  • The third line contains \( n \) space-separated integers.

The input terminates with a line that contains 0 0 (without quotes). The output for each dataset is a single line containing the \( k \) largest values separated by a space, in descending order.

Note: You must read from standard input and write to standard output.

inputFormat

The input will consist of multiple datasets. Each dataset is described as follows:

  • Line 1: An integer \( k \) (the number of largest values to extract).
  • Line 2: An integer \( n \) (the number of elements in the dataset).
  • Line 3: \( n \) space-separated integers.

The sequence of datasets ends when a line containing 0 0 is encountered. This line should not be processed.

outputFormat

For each dataset, output a single line containing the \( k \) largest integers from the dataset, sorted in descending order and separated by a single space.

## sample
3
6
1 23 12 9 30 2
2
5
10 20 11 21 19
0 0
30 23 12

21 20

</p>