Creative Snap (Codeforces 1111C)
Tags: Math, divide and conquer

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 $$$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:   if the current length is at least $$$2$$$, divide the base into $$$2$$$ equal halves and destroy them separately, or  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. Output the minimum power needed by Thanos to destroy the avengers' base.

Input: 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.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.

Output: 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 $$$1-4$$$ with power $$$2 \cdot 2 \cdot 4 = 16$$$.Otherwise he can divide the base into two parts $$$1-2$$$ and $$$3-4$$$.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$$$.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$$$. Similarly, he needs $$$3$$$ power to destroy $$$3-4$$$. The total minimum power needed is $$$6$$$.

Sample Inputs:
2 2 1 2
1 3

3 2 1 2
1 7

Sample Outputs:
6

8

Solution (Python):
(n, num_vingadores, A, B) = [int(e) for e in input().split()]
poder = tuple([int(e) for e in input().split()])
memo = {}

def solve(n, num_vingadores):
    if n == 0 and len(num_vingadores) == 0:
        return A
    elif n == 0:
        return B * len(num_vingadores)
    if len(num_vingadores) != 0:
        opt1 = B * len(num_vingadores) * (1 << n)
    else:
        opt1 = A
    ref = 1 << n - 1
    kl = tuple([e for e in num_vingadores if e <= ref])
    kr = tuple([e - ref for e in num_vingadores if e > ref])
    if (n, kl) in memo:
        l = memo[n, kl]
    else:
        tmp = solve(n - 1, kl)
        memo[n, kl] = tmp
        l = tmp
    if (n, kr) in memo:
        r = memo[n, kr]
    else:
        tmp = solve(n - 1, kr)
        memo[n, kr] = tmp
        r = tmp
    opt2 = l + r
    return min(opt1, opt2)
print(solve(n, poder))

Solution (Python):
(n, k, A, B) = [int(e) for e in input().split()]
p = tuple([int(e) for e in input().split()])
m = {}

def solve(n, k):
    if n == 0 and len(k) == 0:
        return A
    elif n == 0:
        return B * len(k)
    if len(k) != 0:
        op = B * len(k) * (1 << n)
    else:
        op = A
    r = 1 << n - 1
    kl = tuple([e for e in k if e <= r])
    kr = tuple([e - r for e in k if e > r])
    if (n, kl) in m:
        l = m[n, kl]
    else:
        tmp = solve(n - 1, kl)
        m[n, kl] = tmp
        l = tmp
    if (n, kr) in m:
        r = m[n, kr]
    else:
        tmp = solve(n - 1, kr)
        m[n, kr] = tmp
        r = tmp
    op1 = l + r
    return min(op, op1)
print(solve(n, p))

Solution (Python):
(p, k, A, B) = [int(i) for i in input().split()]
pos = tuple([int(i) for i in input().split()])
d = {}

def solve(p, K, A, B):
    if p == 0:
        if len(K) == 0:
            return A
        else:
            return B * len(K)
    if len(K) != 0:
        op1 = B * len(K) * 2 ** p
    else:
        op1 = A
    ref = 2 ** p // 2
    Kl = tuple([e for e in K if e <= ref])
    Kr = tuple([e - ref for e in K if e > ref])
    if (p, Kl) in d:
        LL = d[p, Kl]
    else:
        tmp = solve(p - 1, Kl, A, B)
        d[p, Kl] = tmp
        LL = tmp
    if (p, Kr) in d:
        RR = d[p, Kr]
    else:
        tmp = solve(p - 1, Kr, A, B)
        d[p, Kr] = tmp
        RR = tmp
    op2 = LL + RR
    return min(op1, op2)
print(solve(p, pos, A, B))