#P16896. [GKS 2022 #G] Cute Little Butterfly

    ID: 19224 远端评测题 4000ms 1024MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>动态规划 DP线段树2022Google Kick Start

[GKS 2022 #G] Cute Little Butterfly

Problem Description

In a forest of the magical world, there lies a garden full of magical creatures. The garden has plenty of flowers which are not just beautiful but also a source of energy for butterflies.

Consider the garden a 22D plane where the XX-axis represents the ground, and the YY-axis represents the altitude. There are plants of infinite height on every non-negative integral point on the XX-axis. There are NN flowers in the garden, where the ii-th flower is on the point (Xi,Yi)(X_i, Y_i) with the nectar of some energy value CiC_i.

Our cute little butterfly wants as much energy as possible to become strong. By going to the same position of a flower, the butterfly can consume its nectar and gain that flower's energy value. Each flower's nectar can only be consumed once.

The butterfly is initially at point (0,1018)(0, 10^{18}) with 00 units of energy and facing towards the right. At any point, the butterfly can:

  • Move to a lower altitude, that is, from (x,y)(x, y) to (x,y1)(x, y - 1) only if its current altitude is positive (y>0)(y > 0).
  • Move in the positive direction along the XX-axis, that is, from (x,y)(x, y) to (x+1,y)(x + 1, y) if it is facing right.
  • Move in the negative direction along the XX-axis, that is, from (x,y)(x, y) to (x1,y)(x - 1, y) if it is facing left.
  • Change the direction it is facing (from left to right or vice versa). This will consume EE units of energy.

We know our butterfly is lazy, and it hates to move upwards during the journey. So, for this problem, we will assume that going upwards is not allowed. Also, energy can be negative at any point. Negative energy means the butterfly has spent more energy than it obtained from the flowers.

Find the maximum energy our cute butterfly can achieve.

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 two integers, NN and EE: the number of flowers and the energy required per turn, respectively.

The next NN lines describe the flowers. The ii-th line contains three integers, XiX_i, YiY_i and CiC_i: the position and the energy value of the ii-th flower, 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 overall energy our cute butterfly can achieve.

2
4 10
1 1 2
1 2 2
2 1 2
2 2 2
6 5
1 1 4
1 3 1
3 4 5
4 3 2
5 2 1
3 2 10
Case #1: 6
Case #2: 17

Hint

In sample test case #11, there are N=4N = 4 flowers and E=10E = 10. To maximise the overall energy our butterfly can move in this way:

:::align{center} :::

  • Collect energy from the second flower. Total energy is now 22 units
  • Collect energy from the fourth flower, by moving right. Total energy is now 44 units
  • Collect energy from the third flower, by moving down. Total energy is now 66 units

Hence, the total energy the butterfly got in this way is 66 units.

In sample test case # 22, there are N=6N = 6 flowers and E=5E = 5. To maximise the overall energy our butterfly can move in this way:

:::align{center} :::

  • Collect energy from the third flower. Total energy is now 55 units
  • Collect energy from the fourth flower, by moving right and down. Total energy is now 77 units
  • Collect energy from the fifth flower, by moving right and down. Total energy is now 88 units
  • Change direction to left. Total energy is now 33 units
  • Collect energy from the sixth flower, by moving left. Total energy is now 1313 units
  • Collect energy from the first flower, by moving left and down. Total energy is now 1717 units

Hence, the total energy the butterfly got in this way is 1717 units.

Limits

1T1001 \le T \le 100.

0E1090 \le E \le 10^9.

1Ci1091 \le C_i \le 10^9, for all ii.

All flowers are located at distinct points.

Test Set 1

1N61 \le N \le 6.

0Xi5000 \le X_i \le 500, for all ii.

0Yi5000 \le Y_i \le 500, for all ii.

Test Set 2

1N1031 \le N \le 10^3.

0Xi5000 \le X_i \le 500, for all ii.

0Yi5000 \le Y_i \le 500, for all ii.

Test Set 3

0Xi1050 \le X_i \le 10^5, for all ii.

0Yi1090 \le Y_i \le 10^9, for all ii.

For at most 1010 cases:

1N1051 \le N \le 10^5.

For the remaining cases:

1N1041 \le N \le 10^4.