#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 p1,p2,,pnp_1, p_2, \dots, p_n, then the pain that you will feel from them is mex({p1,p2,,pn})\mathrm{mex}(\{p_1, p_2, \dots, p_n\}), where mex\mathrm{mex} 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 mex\mathrm{mex} of their potentials.

To protect yourself, you bring a shield which can, also through magic, deflect the spells. Your shield has an activation duration dd, which means that when you activate the shield, the next dd spells do not hit you. Afterwards, the shield must regenerate its magic and cannot be activated for the following dd 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 d=2d = 2. 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 N={0,1,2,}\mathbb{N} = \{0, 1, 2, \dots\}.

² Given a set SNS \subseteq \mathbb{N}, SNS \neq \mathbb{N}, mex(S)\mathrm{mex}(S) is the smallest number mNm \in \mathbb{N} that is not contained in SS.

Input Format

The input consists of:

  • One line with two integers nn and dd (1n1051 \leq n \leq 10^5, 1dn1 \leq d \leq n), the number of spells and the activation duration of your shield.
  • One line with nn integers p1,,pnp_1, \dots, p_n (0pi<n0 \leq p_i < n), the potentials of the nn 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 11 hit you. As mex({1,1})=0\mathrm{mex}(\{1, 1\}) = 0, you receive 00 pain.

Sample 2 Explanation

No matter how you activate your shield, during the time it regenerates, a spell of potential 00 and a spell of potential 11 hit you. Thus, the mex\mathrm{mex} of the spells that hit you is at least 22. 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 8,1,1,0,0,1,0,6,8, 1, 1, 0, 0, 1, 0, 6, and 44. The mex\mathrm{mex} of these potentials is 22. It can be shown that there is no way of activating your shield so that the mex\mathrm{mex} of the potentials of the remaining spells is 00 or 11.

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 00. Instead, by blocking the second and third spell, all spells that hit you have potential 00.