#P16727. [GKS 2019 #B] Energy Stones

[GKS 2019 #B] Energy Stones

Problem Description

Duda the rock monster lives in the enchanted forest and has collected NN energy stones for lunch. Since he has a small mouth, he eats energy stones one at a time. Some stones are tougher than others! The ii-th stone takes him SiS_i seconds to eat.

Duda eats energy stones to get energy. Different stones give him different amounts of energy. Furthermore, the stones lose energy over time. The ii-th stone initially contains EiE_i units of energy and will lose LiL_i units of energy each second. When Duda starts to eat a stone, he will receive all the energy the stone contains immediately (no matter how much time it takes to actually finish eating the stone). The stone's energy stops decreasing once it hits zero.

What is the largest amount of energy Duda could receive from eating his stones?

Input Format

The first line of the input gives the number of test cases, TT. TT test cases follow. Each test case starts with a line containing the integer NN, the number of energy stones Duda has. Then, there are NN more lines, the ii-th of which contains the three integers SiS_i, EiE_i and LiL_i, as described above.

Output Format

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the maximum amount of energy Duda could receive from eating stones.

2
3
10 4 1000
10 3 1000
10 8 1000
2
10 2 0
10 3 0
Case #1: 8
Case #2: 5
2
4
20 10 1
5 30 5
100 30 1
5 80 60
2
12 300 50
5 200 0
Case #1: 105
Case #2: 500

Hint

Sample

In Sample Case #1, there are N=3N = 3 stones. No matter which stone Duda eats, the other two will have no energy left once he is done eating. So he should eat the third stone, giving him 88 units of energy.

Additional Sample - Test Set 2

The following additional sample fits the limits of Test Set 2. It will not be run against your submitted solutions.

In Sample Case #1, there are N=4N = 4 stones. One possible order Duda can eat stones is:

  • Eat the fourth stone. This takes 55 seconds and gives him 8080 units of energy.
  • Eat the second stone. This takes 55 more seconds and gives him 55 units of energy (the second stone started with 3030 energy, and over 55 seconds, has lost 2525 units of energy).
  • Eat the third stone. This takes 100100 more seconds and gives him 2020 units of energy (the third stone started with 3030 energy, and over 1010 seconds, has lost 1010 units of energy).
  • Eat the first stone. This takes 2020 more seconds and gives him 00 units of energy (the first stone started with 1010 units of energy, and over 110110 seconds, has lost all of its energy).

This gives him 105105 units of energy, which is the best he can do. So the answer is 105105.

In Sample Case #2, there are N=2N = 2 stones. Duda can:

  • Eat the first stone. This takes 1212 seconds and gives him 300300 units of energy.
  • Eat the second stone. This takes 55 seconds and gives him 200200 units of energy (the second stone does not lose any energy over time!).

So the answer is 500500.

Limits

1T1001 \le T \le 100.

1N1001 \le N \le 100.

1Si1001 \le S_i \le 100.

1Ei1051 \le E_i \le 10^5.

0Li1050 \le L_i \le 10^5.

Test set 1 (Visible)

All stones take the same amount of time to eat. That is: Si=SjS_i = S_j for all ii and jj.

Test set 2 (Hidden)

There are no additional constraints beyond the general Limits.