Ice-cream Knapsack

Time limit5sMemory limit512 MB

Summary
Choose exactly K ice creams so the largest calorie count among them is minimized, breaking ties by the largest total happiness, and report both values.
Level

Medium6 of 10

Topics
Greedy, Sorting, Heap, Binary search
Solved
No attempts yet

Problem

There is a wonderful ice-cream shop that contains N ice-creams, such that each ice-cream is represented by two numbers CiC_i and HiH_i denoting the number of calories and the happiness value, respectively.

You want to buy exactly K ice-creams such that the calories of the densest ice-cream (the one with most calories) are as minimal as possible. If there is more than one way to do that, you want to maximize the total happiness of the ice-creams you will buy, that is the sum of the happiness values of the chosen ice-creams.

Input

The first line of the input contains a single integer T specifying the number of test cases.

Each test case begins with a line containing two integers N and K (1 ≤ K ≤ N ≤ 10^5), in which N is the number of ice-creams in the shop, and K is the number of ice-creams you want to buy.

Then a line follows containing N integers C1,⋯ ,CNC_1, \cdots, C_N (0 ≤ CiC_i ≤ 10^9), in which CiC_i is the number of calories in the ith ice-cream. Then a line follows containing N integers H1,⋯ ,HNH_1, \cdots, H_N (0 ≤ HiH_i ≤ 10^9), in which HiH_i is the happiness value of the ith ice-cream.

Output

For each test case, print a single line containing two space-separated integers representing the calories of the densest ice-cream you will buy and the total happiness of the ice-creams you will buy, respectively.

Remember that your goal is to buy K ice-creams such that the calories of the densest ice-cream (the one with most calories) are as minimal as possible. If there is more than one way to do that, you want to maximize the total happiness of the ice-creams you will buy.

Examples1

  1. Example 1

    Input
    1
    5 3
    1 2 3 4 5
    5 4 3 2 1
    
    Expected output
    3 12