#C7212. Most Popular Cakes

    ID: 51059 Type: Default 1000ms 256MiB

Most Popular Cakes

A bakery collects orders for various types of cakes. Each cake is identified by a unique identifier and has a certain order count indicating how many times it was ordered. Given the total number of different cakes n and an integer k, your task is to determine the top k most popular cakes based on their order counts.

Let \( C_i \) denote the order count of the \( i^{th} \) cake. Define \( C_k \) as the \( k^{th} \) highest order count when the cakes are sorted in descending order of \( C_i \). If there are multiple cakes with an order count equal to \( C_k \), then all such cakes should be included in the output. The output should include the identifiers of these cakes (in any order) and the total sum of their orders.

Input/Output Format:

The input is read from standard input (stdin) and the output is written to standard output (stdout).

inputFormat

The first line contains two integers n and k separated by a space, where n is the number of cakes and k is the number of top cakes to select.

This is followed by n lines, each containing two integers representing a cake's identifier and its order count.

Example:

5 2
101 150
102 300
103 200
104 300
105 150

outputFormat

The output consists of two lines:

  • The first line contains the identifiers of the cakes that are among the top k most popular (if there is a tie at the k-th place, include all cakes with that order count). The identifiers are separated by spaces.
  • The second line contains a single integer representing the total number of orders for the selected cakes.

Example Output:

102 104
600
## sample
5 2
101 150
102 300
103 200
104 300
105 150
102 104

600

</p>