#P15340. [GCPC 2025] Mex Hex
[GCPC 2025] Mex Hex
Description
The peace and quiet of your home city is being disturbed by the loud purrs and nightly creeping of a nearby magic bobcat. The mayor is sending you to scare it off – you are the city’s Guardian for Collateral, Personal, and other Catastrophic Damages after all. You take on the challenge and prepare for scaring away the magnificent animal.
To defend itself against you, the magic bobcat (as the name suggests) tries to hurt you in unconventional ways. When it attacks, it casts a number of magic spells. Each spell is cast with a particular potential, which can be expressed as a natural number.¹ If you get hit with spells that have the potentials , then the pain that you will feel from them is , where denotes the Minimum Excluded Value.² Note that the spells do not hurt you immediately. Only after all spells are cast will you feel the pain based on the of their potentials.
To protect yourself, you bring a shield which can, also through magic, deflect the spells. Your shield has an activation duration , which means that when you activate the shield, the next spells do not hit you. Afterwards, the shield must regenerate its magic and cannot be activated for the following spells. Apart from that, you can activate the shield as often as you want. To ensure that you give the magic bobcat a good scare, the mayor requested that you sneak up to it. As the bobcat would spot the glow of your activated magic shield, the earliest time you can activate the shield is when you stand before the culprit, right before it casts the first spell.
:::align{center}

Figure M.1: Illustration of Sample 3. The numbers in the boxes indicate the potentials of the spells. In this example, your shield’s activation duration is . By activating the shield right before the 5th, 9th, and 14th spell, you block the spells underlined in green. You cannot activate your shield for the spells that are underlined with a dashed red line, because it is regenerating its magic at these times. :::
You must now devise a tactic to sustain as little pain as possible. From studying the previous encounters with magic bobcats, you know the potentials of the spells that will be cast against you. What is the minimum pain you can receive from them if you activate your shield optimally?
¹ For the purposes of this task, the natural numbers are defined as .
² Given a set , , is the smallest number that is not contained in .
Input Format
The input consists of:
- One line with two integers and (, ), the number of spells and the activation duration of your shield.
- One line with integers (), the potentials of the spells.
Output Format
Output a single integer, the minimum pain you can receive from the spells.
5 1
0 1 0 1 0
0
5 2
0 1 0 1 0
2
14 2
8 1 1 0 0 2 0 1 2 1 0 6 4 2
2
4 2
0 1 2 0
1
Hint
Sample 1 Explanation
You can activate the shield for the first spell, let it regenerate its magic for the second, activate it again for the third, regenerate for the fourth, and activate again on the fifth spell. This means that only the two spells of potential hit you. As , you receive pain.
Sample 2 Explanation
No matter how you activate your shield, during the time it regenerates, a spell of potential and a spell of potential hit you. Thus, the of the spells that hit you is at least . This amount of pain can be achieved by not using your shield at all.
Sample 3 Explanation
Use the shield as depicted in Figure M.1. Then the spells that hit you have potentials and . The of these potentials is . It can be shown that there is no way of activating your shield so that the of the potentials of the remaining spells is or .
Sample 4 Explanation
Recall that you cannot activate your shield any earlier than right before the first spell. Thus, you cannot block both spells of potential . Instead, by blocking the second and third spell, all spells that hit you have potential .
京公网安备 11011102002149号