#P16891. [GKS 2022 #F] Story of Seasons

[GKS 2022 #F] Story of Seasons

Problem Description

You are a super farmer with some vegetable seeds and an infinitely large farm. In fact, not only are you a farmer, but you are also secretly a super programmer! As a super programmer, you hope to maximize the profit of your farming using your programming skills.

Since your daily energy is limited, you can plant at most XX seeds each day. In the beginning, you have NN kinds of vegetable seeds. The number of seeds of the ii-th kind of vegetable is QiQ_i, and each seed of this kind needs LiL_i days to mature from the day it is planted. Once it matures, you can sell it for ViV_i dollars. Assume that no energy or time is required for harvesting and selling vegetables. Also, your farm is infinitely large so the growing vegetables do not crowd out each other.

Notice that although the area of your farm is infinite, the number of days that you can plant seeds is limited. The warm season only lasts DD days, and after that, the harsh winter comes. Any vegetable that has not matured yet will die immediately and cannot be turned into profit. The remaining seeds that were not planted cannot be turned into profit either.

As a super farmer and a super programmer, you want to come up with a perfect planting plan that will maximize your profit. Find the total amount of profit you will earn.

Input Format

The first line of the input gives the number of test cases, TT. TT test cases follow.

The first line of each test case contains three integers DD, NN, and XX: the number of days of the warm season, the number of kinds of vegetable seeds you have to start with, and the maximum number of seeds you can plant each day, respectively.

The next NN lines describe the seeds. The ii-th line contains three integers QiQ_i, LiL_i, and ViV_i: the quantity of this kind of seed, the number of days it needs to mature, and the value of each matured plant, respectively.

Output Format

For each test case, output one line containing Case #xx: yy, where xx is the test case number (starting from 11) and yy is the maximum amount of money you can earn by optimizing your farming plan.

2
5 4 1
1 2 3
1 3 10
1 4 5
1 2 2
5 1 1
1 1 1
Case #1: 18
Case #2: 1
1
5 3 4
5 2 3
2 3 10
2 4 5
Case #1: 45

Hint

In Sample Case #11, there are D=5D = 5 days, N=4N = 4 kinds of vegetables, and you can plant at most X=1X = 1 seed each day. Supposing the 44 kinds of vegetables are spinach, pumpkin, carrot, and cabbage, we have that

  • Spinach needs 22 days to grow, each matured one is worth 33 dollars, and you start with 11 spinach seed.
  • Pumpkin needs 33 days to grow, each matured one is worth 1010 dollars, and you start with 11 pumpkin seed.
  • Carrot needs 44 days to grow, each matured one is worth 55 dollars, and you start with 11 carrot seed.
  • Cabbage needs 22 days to grow, each matured one is worth 22 dollars, and you start with 11 cabbage seed.

The maximum profit you can make is 1818 dollars. One of the schedules you can use is:

  • Day 11: plant 11 carrot
  • Day 22: plant 11 pumpkin
  • Day 33: plant 11 spinach
  • Day 44: plant 11 cabbage
  • Day 55: do nothing

And with this schedule, the vegetables will mature and turn into profit as following days:

  • Day 11: nothing harvested
  • Day 22: nothing harvested
  • Day 33: nothing harvested
  • Day 44: nothing harvested
  • Day 55: 11 spinach, 11 pumpkin, and 11 carrot harvested; you earn 1818 dollars

Note that the cabbage is supposed to mature on day 66, but it will actually die because of the winter.

In Additional Sample Case #11, there are D=5D = 5 days, N=3N = 3 kinds of vegetables, and you can plant at most X=4X = 4 seeds each day. Supposing the 33 kinds of vegetables are spinach, pumpkin, and carrot, we have that

  • Spinach needs 22 days to grow, each matured one is worth 33 dollars, and you start with 55 spinach seeds.
  • Pumpkin needs 33 days to grow, each matured one is worth 1010 dollars, and you start with 22 pumpkin seeds.
  • Carrot needs 44 days to grow, each matured one is worth 55 dollars, and you start with 22 carrot seeds.

The maximum profit you can make is 4545 dollars. One of the schedules you can use is:

  • Day 11: plant 11 pumpkin, 22 carrots and 11 spinach
  • Day 22: plant 22 spinach and 11 pumpkin
  • Day 33: plant 22 spinach
  • Day 44: do nothing
  • Day 55: do nothing

And with this schedule, the vegetables will mature and turn into profit as following days:

  • Day 11: nothing harvested
  • Day 22: nothing harvested
  • Day 33: 11 spinach harvested, you earn 33 dollars
  • Day 44: 22 spinach and 11 pumpkin harvested, you earn 1616 dollars
  • Day 55: 22 spinach, 11 pumpkin and 22 carrots harvested, you earn 2626 dollars

Limits

Memory limit: 11 GB.

1T1001 \le T \le 100.

1Vi1061 \le V_i \le 10^6, for all ii.

1LiD1 \le L_i \le D, for all ii.

Test Set 11

2D10002 \le D \le 1000.

1N151 \le N \le 15.

X=1X = 1.

Qi=1Q_i = 1, for all ii.

Test Set 22

2D1052 \le D \le 10^5.

1N1051 \le N \le 10^5.

1X1091 \le X \le 10^9.

1Qi1061 \le Q_i \le 10^6, for all ii.

Test Set 33

2D10122 \le D \le 10^{12}.

1N1051 \le N \le 10^5.

1X1091 \le X \le 10^9.

1Qi1061 \le Q_i \le 10^6, for all ii.

D×X1018D \times X \le 10^{18}.