Cherry Picking

Time limit1sMemory limit128 MB

Summary
Choose one premium per category so at least m patients are insured and total premiums minus benefits is maximized.
Level

Medium7 of 10

Topics
Dynamic programming, Sorting, Greedy, Combinatorics
Solved
No attempts yet

Problem

Insurance began as a simple idea: everyone pays money into a common pool so that, if one or a few members suffer a calamity they could not afford on their own, there is enough money to cover them. As participation grew, administration became more complex and more rules were needed. Eventually people argued that this administration would run far more efficiently under private companies motivated by profit. But once a private insurer tries to maximize profit, its incentives change: it has the least interest in covering exactly the people who need insurance most, such as the elderly or patients with pre-existing conditions. Choosing to insure only the most profitable individuals is known as cherry picking. In this problem you will write a program that cherry-picks well.

You are given a list of nn patients, each belonging to one of CC categories (for example, "male, 18-30" or "female, 25-40"). For every patient you know the maximum premium they can afford per year and the amount of benefits you expect to pay them per year. For each category cc you may set a single premium pcp_c. Within category cc, every patient whose affordable amount is at least pcp_c becomes insured: they pay you pcp_c per year, and in return you must pay their benefits. Patients who cannot afford pcp_c go elsewhere and are not insured.

Your profit is the total premiums collected minus the total benefits paid. You want to choose the premiums to maximize this profit. However, a regulator requires that you insure at least mm patients in total, so your choice of premiums must insure at least mm people.

Input

The first line contains the number of data sets KK. Each of the KK data sets has the following form.

The first line of a data set contains three integers nn, CC, and mm: 1≤n≤10001 \le n \le 1000 is the number of patients, 1≤C≤301 \le C \le 30 is the number of categories, and 0≤m≤n0 \le m \le n is the minimum number of patients you must insure.

Each of the next nn lines describes one patient ii with three integers cic_i, pip_i, and bib_i: 1≤ci≤C1 \le c_i \le C is the patient's category, pi≥0p_i \ge 0 is the maximum premium the patient can afford, and bi≥0b_i \ge 0 is the benefits you must pay that patient.

Output

For each data set, print a line Data Set x:, where xx is the 1-based index of the data set. On the following line print a single integer: the maximum profit you can make by choosing one premium per category while insuring at least mm patients. Separate consecutive data sets with a single blank line, and do not print a blank line after the last data set. The maximum profit may be negative.

Examples4

  1. Example 1

    Input
    1
    8 4 5
    1 300 500
    1 500 400
    1 600 0
    3 999 0
    4 1000 1500
    3 1000 99273
    2 50 60
    2 50 70
    
    Expected output
    Data Set 1:
    70
    
  2. Example 2

    Input
    1
    1 1 0
    1 100 30
    
    Expected output
    Data Set 1:
    70
    
  3. Example 3

    Input
    1
    2 1 2
    1 10 100
    1 20 5
    
    Expected output
    Data Set 1:
    -85
    
  4. Example 4

    Input
    2
    1 1 0
    1 100 30
    2 1 2
    1 10 100
    1 20 5
    
    Expected output
    Data Set 1:
    70
    
    Data Set 2:
    -85