## Editorial for LCC '21 Contest 1 J4 - Explosive Candygrams

**only**when stuck, and

**not to copy-paste code from it**. Please be respectful to the problem author and editorialist.

**Submitting an official solution before solving the problem yourself is a bannable offence.**

Author:

#### Subtask 1

For a candygram and , we know that the number of candygrams in between is . Therefore, for each candygram, we can loop through all other candygrams. If the number of candygrams between the two is more than or equal to , then we know that the current candygram can be hit. We then take the maximum of all candygrams we can hit. If we cannot hit any candygrams, then we instead take

#### Subtask 2

Consider the above image, where we are calculating the answer for the sixth candygram, the one with the second "2" in its cell. Orange cells denote candygrams which the candygram cannot hit (they are all either the current candygram, have 0 candygrams in between, or 1 candygram in between). The important thing to note is that we "split" the current array into 3 sections, where we must consider all candygrams in the yellow section.

For the current candygram , all candygrams with index , such that can be hit, as they have at least candygrams in between. Similarly, all candygrams with index , such that can be hit, as they have at least candygrams in between.

We can use a prefix maximum array to quickly calculate the maximum *explosiveness* on the left side, and a suffix maximum array to quickly calculate the maximum *explosiveness* on the right side. Let us call the prefix maximum array and the suffix maximum array

Then, the annoyance of the current candygram is , making sure to account for when we go out of bounds (i.e, is before the first element, or is past the final element). Furthermore, if we cannot hit any candygram, then the annoyance is

## Comments