CF BUDDY
← Problems·

1111C · Creative Snap

1700 · binary search, brute force, divide and conquer

Problem: Thanos wants to destroy the avengers base, but he needs to destroy the avengers along with their base.

Let we represent their base with an array, where each position can be occupied by many avengers, but one avenger can occupy only one position. Length of their base is a perfect power of 22. Thanos wants to destroy the base using minimum power. He starts with the whole base and in one step he can do either of following:

  • if the current length is at least 22, divide the base into 22 equal halves and destroy them separately, or
  • burn the current base. If it contains no avenger in it, it takes AA amount of power, otherwise it takes his BnalB \cdot n_a \cdot l amount of power, where nan_a is the number of avengers and ll is the length of the current base.

Input Format: The first line contains four integers nn, kk, AA and BB (1n301 \leq n \leq 30, 1k1051 \leq k \leq 10^5, 1A,B1041 \leq A,B \leq 10^4), where 2n2^n is the length of the base, kk is the number of avengers and AA and BB are the constants explained in the question.

The second line contains kk integers a1,a2,a3,,aka_{1}, a_{2}, a_{3}, \ldots, a_{k} (1ai2n1 \leq a_{i} \leq 2^n), where aia_{i} represents the position of avenger in the base.

Output Format: Output one integer — the minimum power needed to destroy the avengers base.

Note: Consider the first example.

One option for Thanos is to burn the whole base 141-4 with power 224=162 \cdot 2 \cdot 4 = 16.

Otherwise he can divide the base into two parts 121-2 and 343-4.

For base 121-2, he can either burn it with power 212=42 \cdot 1 \cdot 2 = 4 or divide it into 22 parts 111-1 and 222-2.

For base 111-1, he can burn it with power 211=22 \cdot 1 \cdot 1 = 2. For 222-2, he can destroy it with power 11, as there are no avengers. So, the total power for destroying 121-2 is 2+1=32 + 1 = 3, which is less than 44.

Similarly, he needs 33 power to destroy 343-4. The total minimum power needed is 66.

Sample Cases

Case 1

Input

2 2 1 2
1 3

Output

6

Case 2

Input

3 2 1 2
1 7

Output

8

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback