## GUPTA MECHANICAL

IN THIS WEBSITE I CAN TELL ALL ABOUT TECH. TIPS AND TRICKS APP REVIEWS AND UNBOXINGS ALSO TECH. NEWS .............

# [Solution] Cute Little Butterfly Round G 2022 Solution - Kick Start 2022

### Problem

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 2D plane where the X-axis represents the ground, and the Y-axis represents the altitude. There are plants of infinite height on every non-negative integral point on the X-axis. There are $N$ flowers in the garden, where the $i$-th flower is on the point (${\mathbf{X}}_{\mathbf{i}}$${\mathbf{Y}}_{\mathbf{i}}$) with the nectar of some energy value ${\mathbf{C}}_{\mathbf{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 $\left(0,{10}^{18}\right)$ with $0$ units of energy and facing towards the right. At any point, the butterfly can:

• Move to a lower altitude, that is, from $\left(x,y\right)$ to $\left(x,y-1\right)$ only if its current altitude is positive ($y>0$).
• Move in the positive direction along the X-axis, that is, from $\left(x,y\right)$ to $\left(x+1,y\right)$ if it is facing right.
• Move in the negative direction along the X-axis, that is, from $\left(x,y\right)$ to $\left(x-1,y\right)$ if it is facing left.
• Change the direction it is facing (from left to right or vice versa). This will consume $E$ 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

The first line of the input gives the number of test cases, $T$$T$ test cases follow.
The first line of each test case contains two integers, $N$ and $E$: the number of flowers and the energy required per turn, respectively.
The next $N$ lines describe the flowers. The $i$-th line contains three integers, ${\mathbf{X}}_{\mathbf{i}}$${\mathbf{Y}}_{\mathbf{i}}$ and ${\mathbf{C}}_{\mathbf{i}}$: the position and the energy value of the $i$-th flower, respectively.

Solution Click Below:-  👉
👇👇👇👇👇

### Output

For each test case, output one line containing Case #x$x$: y$y$, where $x$ is the test case number (starting from 1) and $y$ is the maximum overall energy our cute butterfly can achieve.

### Limits

Memory limit: 1 GB.
$1\le \mathbf{T}\le 100$.
$0\le \mathbf{E}\le {10}^{9}$.
$1\le {\mathbf{C}}_{\mathbf{i}}\le {10}^{9}$, for all $i$.
All flowers are located at distinct points.

#### Test Set 1

Time limit: 20 seconds.
$1\le \mathbf{N}\le 6$.
$0\le {\mathbf{X}}_{\mathbf{i}}\le 500$, for all $i$.
$0\le {\mathbf{Y}}_{\mathbf{i}}\le 500$, for all $i$.

#### Test Set 2

Time limit: 40 seconds.
$1\le \mathbf{N}\le {10}^{3}$.
$0\le {\mathbf{X}}_{\mathbf{i}}\le 500$, for all $i$.
$0\le {\mathbf{Y}}_{\mathbf{i}}\le 500$, for all $i$.

#### Test Set 3

Time limit: 60 seconds.
$0\le {\mathbf{X}}_{\mathbf{i}}\le {10}^{5}$, for all $i$.
$0\le {\mathbf{Y}}_{\mathbf{i}}\le {10}^{9}$, for all $i$.
For at most 10 cases:
$1\le \mathbf{N}\le {10}^{5}$.
For the remaining cases:
$1\le \mathbf{N}\le {10}^{4}$.