Overplay Your Hand
Practice
0 (0 votes)
Medium
Problem
25% Success 101 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

Today Alex and Frank are playing a famous game named "Overplay Your Hand".

There are N boxes each containing different gifts .The cost price and selling price of ith box is \(c_i\) and \(g_i\) respectively.

Alex starts the game by purchasing a particular box. According to the game rules Frank can reduce the selling price of the box that Alex purchases, to 0 ( Thereby wasting Alex's money ). As the selling price of the box is reduced to 0, it is waste and thereby forcing Alex to pick another box.

Alex can purchase at most 1 box whose selling price has not been reduced to 0 by Frank

Frank has at most R turns. He may refrain from using his turn(i.e does not reduce selling price to 0) thereby allowing Alex to take away the box and the game ends. But he would want to minimize Alex's total profit and Alex's aim is to maximize her total profit ( selling price of the box - all the previous expenditure in buying the boxes ).

Alex can quit the game without buying anything. ( Only when there is no chance of getting profit )

You need to help Alex and tell her the maximum profit she can earn when both Alex and Frank use the optimal strategy.

Note: Whenever Frank uses his turn, the selling price will reduce to 0 but the buying price will remain same so there is still cost incurred in buying the box ( previous expenditure )

Input Format :

The first line contains T, number of testcases.

The second line of input contains two integers N and R denoting the number of boxes and the maximum number of turns that Frank is allowed respectively.

N lines follow where ith line contains two integers G and D, denoting the Selling Price and the Cost price respectively of the ith box.

Output Format :

Output the maximum profit that Alex can earn if everyone plays optimally.

Constraints :

1 <= T <= 100

1 <= N <= 200000

0 <= R <= 10

0 <= G, D <= 10^7

Setter : Akshat Dua

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
3 votes
Tags:
MediumAlgorithmsStringDP
Points:30
2 votes
Tags:
OpenDynamic ProgrammingAlgorithmsExpectationApprovedMedium
Points:30
8 votes
Tags:
Advanced Data StructuresDynamic Programming and Bit MaskingAdvanced AlgorithmsAlgorithmsDynamic Programming
Editorial

No editorial available for this problem.