1111C.md (2278B)
1 Source: https://codeforces.com/problemset/problem/1111/C 2 3 C. Creative Snap 4 5 time limit per test 6 7 1 second 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 Thanos wants to destroy the avengers base, but he needs to destroy the avengers along with their base. 22 23 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 $2$. 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: 24 25 * if the current length is at least $2$, divide the base into $2$ equal halves and destroy them separately, or 26 * burn the current base. If it contains no avenger in it, it takes $A$ amount of power, otherwise it takes his $B \cdot n_a \cdot l$ amount of power, where $n_a$ is the number of avengers and $l$ is the length of the current base. 27 28 Output the minimum power needed by Thanos to destroy the avengers' base. 29 30 Input 31 32 The first line contains four integers $n$, $k$, $A$ and $B$ ($1 \leq n \leq 30$, $1 \leq k \leq 10^5$, $1 \leq A,B \leq 10^4$), where $2^n$ is the length of the base, $k$ is the number of avengers and $A$ and $B$ are the constants explained in the question. 33 34 The second line contains $k$ integers $a_{1}, a_{2}, a_{3}, \ldots, a_{k}$ ($1 \leq a_{i} \leq 2^n$), where $a_{i}$ represents the position of avenger in the base. 35 36 Output 37 38 Output one integer — the minimum power needed to destroy the avengers base. 39 40 Examples 41 42 Input 43 44 ``` 45 2 2 1 2 46 1 3 47 ``` 48 49 Output 50 51 ``` 52 6 53 ``` 54 55 Input 56 57 ``` 58 3 2 1 2 59 1 7 60 ``` 61 62 Output 63 64 ``` 65 8 66 ``` 67 68 Note 69 70 Consider the first example. 71 72 One option for Thanos is to burn the whole base $1-4$ with power $2 \cdot 2 \cdot 4 = 16$. 73 74 Otherwise he can divide the base into two parts $1-2$ and $3-4$. 75 76 For base $1-2$, he can either burn it with power $2 \cdot 1 \cdot 2 = 4$ or divide it into $2$ parts $1-1$ and $2-2$. 77 78 For base $1-1$, he can burn it with power $2 \cdot 1 \cdot 1 = 2$. For $2-2$, he can destroy it with power $1$, as there are no avengers. So, the total power for destroying $1-2$ is $2 + 1 = 3$, which is less than $4$. 79 80 Similarly, he needs $3$ power to destroy $3-4$. The total minimum power needed is $6$.