We need to compute the minimum power required to destroy the base: The base is an array of length L = 2^n. Positions are from 1 to 2^n inclusive. There are k avengers at positions given. The process: we consider a segment (continuous) of length l (which is a power of two? Actually, the process only divides into equal halves, so all considered segments are subarrays of lengths that are powers of two, aligned to boundaries? Starting from whole base, which is length 2^n, and dividing into halves (by index) yields subsegments that are aligned to the original partition. So effectively, we have a binary tree over the base: each node corresponds to an interval of length 2^d (for some depth), starting at positions that are multiples of that length. The process allows at each node: either burn the whole segment (i.e., treat as leaf) paying cost: if no avenger in that segment => cost A; else cost B * (number of avengers in that segment) * (length of segment). Or split the segment into two halves (if length >=2) and recursively destroy each half, summing their costs.
We need to compute the minimal total cost for the root segment.
This is a classic divide and conquer dp problem. Since n ≤ 30, total length up to 2^30 (~1e9) but k ≤ 1e5. We cannot traverse the whole tree. But we can use the avengers positions and process recursively: at each segment we need to know the number of avengers in it. Since avengers are unique positions? "each position can be occupied by many avengers, but one avenger can occupy only one position." Means: each avenger has a unique position, and multiple avengers can be at same position? Actually "each position can be occupied by many avengers, but one avenger can occupy only one position." This is contradictory? It likely means: a position may have many avengers, but an avenger can only be at one position. So there can be multiple avengers at the same coordinate. But then the input provides k integers a_i representing positions; they can repeat? The statement: "The second line contains k integers a_1, ..., a_k (1 ≤ a_i ≤ 2^n), where a_i represents the position of avenger in the base." It doesn't explicitly say they are distinct. So we need to handle duplicates: at a given position, there could be multiple avengers. So we need to count frequencies.
Thus we need to answer for any segment [L, R] (aligned to power-of-two boundaries at each recursion) the number of avengers inside. Since the tree depth is up to 30, and total nodes visited can be limited if many segments are empty. In worst-case, if k = 10^5, and they are scattered, the recursion will explore many nodes: each avenger lies at a leaf position (length 1). The full binary tree of height n has about 2^(n+1)-1 nodes, which is huge. However, we can prune: If a segment contains no avengers, we can just return A (since burning empty segment costs A, and there's no benefit to split because splitting an empty segment: each half also empty, cost would be A + A = 2A (if we split all the way, sum of leaf costs = 2^something * A) which is larger than just burning it at this level? Actually we need to compare: For empty segment of length l, the cost if we burn directly is A. If we split, each half of length l/2 is also empty, so recursively each half will have cost at least A (since burning directly). So total cost for splitting would be at least 2A. Since A > 0, burning directly is cheaper. So optimal for empty segments is to burn at that level (no need to split). So we can treat empty segments as base case returning A.
If the segment contains some avengers, we have two options: either burn directly (cost = B * count * l) or split (cost = cost(left) + cost(right)). We need to compute minimal.
Therefore we can recursively compute function solve(l, r) where l and r are boundaries of a segment whose length is a power of two. The root is (1, 2^n). We'll need to quickly count number of avengers in the segment. Since k up to 1e5, we can sort the positions and then use binary search to get count in [l, r].
But we must be careful: if we blindly split every segment that has avengers, we might generate many nodes. In worst-case scenario, if avengers are densely populated, we might need to traverse many nodes, but the number of nodes visited will be bounded by O(k * n) perhaps. Let's analyze: Each avenger occupies a leaf. In recursion, we explore nodes that contain at least one avenger. However, the tree is binary. In a binary tree, the number of nodes that contain at least one marked leaf is at most O(k * height) if the leaves are sparse? Actually each avenger can contribute to at most n nodes on the path from root to leaf (since each leaf corresponds to a unique path of length n). So total nodes visited (with non-zero avengers) is at most O(k * n) if we treat each avenger individually. But if avengers are in the same path, nodes overlap. The total distinct nodes that contain at least one avenger can be up to O(k * n) but also could be less. k = 1e5, n = 30, so O(3e6) nodes is feasible. However, each node requires a binary search count query: for each segment [l, r], we need count of avengers in that range. We could compute count by passing the list of positions in that subsegment if we build recursively using indices into the sorted array. Another approach: we can use recursion that takes as arguments the segment boundaries and also the range of indices in the global sorted list that fall within that segment. This way we avoid binary searches each time; we can simply maintain the count as length of that subarray. Since we need count many times, we can pre-sort positions, and in recursion we pass the start and end indices (in the sorted list) that correspond to the current segment. Then count = end - start. That's efficient.
Algorithm:
- Read n, k, A, B.
- L = 2^n (use 1 << n, but careful: n up to 30, 2^30 fits in 64-bit integer, but may exceed int32; use 64-bit (long long)).
- Read k positions into an array pos. Since duplicates allowed, we should include them all, because each avenger contributes to count. So we keep all positions. Sort pos.
- Define recursive function f(l, r, lo, hi) where [l, r] is current segment (inclusive? I'd use inclusive: l to r, length = r - l + 1). lo is index in pos of first element >= l? Actually we can pass the indices in the sorted array that are within this segment: all positions from lo to hi-1 (i.e., hi exclusive) are in [l, r]. So count = hi - lo.
- Base: If count == 0: return A. (since no avengers)
- Else: compute cost_burn = B * count * (r - l + 1).
- If l == r (segment length 1): we cannot split; return cost_burn. (But splitting not allowed if length 1? The statement: "if the current length is at least 2, divide the base into 2 equal halves". So when length=1, only option is burn. So we can return cost_burn directly.
- Else: split: mid = (l + r) / 2. We need to find the dividing point in the sorted positions: we can binary search for first position > mid, or better, we can partition the indices: Since pos is sorted and we know all positions in [l, r] are between lo and hi-1, we can find split index mid_idx such that positions < = mid are in left, others in right. Use binary search: lower_bound in the subarray? But we can do: while leftIdx < hi and pos[leftIdx] <= mid? Actually we can compute using std::upper_bound on pos.begin()+lo, pos.begin()+hi, mid) to get the first index > mid. Let's call split = that. Then left indices: [lo, split), right: [split, hi).
- Then cost_left = f(l, mid, lo, split)
- cost_right = f(mid+1, r, split, hi)
- cost_split = cost_left + cost_right
- Return min(cost_burn, cost_split)
Complexities: Recursion depth up to n=30. For each node, we perform a binary search on the subarray (size up to count) which is O(log count). Total binary search overhead O(number of nodes * log k). With k up to 1e5 and nodes up to maybe 2e6, that could be okay but might be heavy. But we can avoid binary search by simply scanning? Actually, if we are careful, we can pass the subarray indices and just need to split the range. The split point can be found by scanning from lo to hi until pos[i] > mid? That could be O(count) per node, which would be too slow. Better use binary search. Since we have the global sorted array, we can compute split index by using std::upper_bound with iterators. That's O(log k) per node. With up to maybe a few million nodes, O(log k) ~ 17, so total ~ 50 million operations, might be okay in C++. But we need to ensure we don't have too many nodes. The worst-case number of nodes visited: For each avenger, we would traverse the path from root to leaf. But the recursion will also visit nodes that contain multiple avengers, and those nodes will be processed only once. So total nodes visited equals the number of distinct nodes in the binary tree that contain at least one avenger. In worst-case, if avengers are all distinct and placed such that they fill the tree densely, the number of nodes with avengers could be up to something like: For a full binary tree with 2^n leaves, if all leaves have at least one avenger, then every node in the tree contains at least one avenger. The total nodes = 2^(n+1)-1. For n=30, that's about 2 billion, which is impossible. But k is only up to 10^5, so we cannot have all leaves occupied. So the number of nodes visited is limited by k * (some factor). Let's bound more precisely: In a binary tree, the number of nodes that are "alive" (i.e., contain at least one marked leaf) when we have k marked leaves is at most O(k * log (total leaves / k))? Actually, consider each marked leaf, its ancestors. The set of nodes containing at least one marked leaf is the union of paths from root to each marked leaf. In a binary tree, the number of distinct nodes in the union of k root-to-leaf paths is at most k * (height) - O(k^2) overlapping? In worst case, if the leaves are spread out, the paths may share few nodes. The maximum distinct nodes is when the leaves are as far apart as possible, minimizing overlap. The minimal overlap is when the leaves are in distinct subtrees as much as possible. The root is always shared. Then each child may be shared if two leaves are in the same subtree. The worst-case distinct count is when the leaves are all in different branches at the deepest level, but they still share ancestors near the root. Actually, to maximize number of distinct nodes, we want the paths to be as disjoint as possible. In a full binary tree, the maximum number of nodes that can be covered by k leaves is achieved by selecting leaves that are as far apart as possible, i.e., taking leaves that are all in different subtrees at each level. At root, 1 node. At depth 1, we might have up to 2 nodes if leaves are in both halves. At depth 2, up to 4 nodes, etc. But since we only have k leaves, the maximum number of distinct nodes is: sum_{i=0}^{n} min(2^i, k * something?) Actually, consider each level of the tree. At depth d (0 = root), there are 2^d nodes. The number of nodes at depth d that have at least one marked leaf in their subtree is at most min(2^d, k) because each node covers a disjoint set of leaves (at depth n, the leaves are disjoint across nodes at that depth). However, for shallower depths, each node covers many leaves, but the condition for a node to be present is that at least one leaf in its subtree is marked. So at depth d, the maximum possible number of nodes that can be "activated" is min(2^d, k). Because if you have k marked leaves, you can at most have k nodes at depth d that are ancestors of those leaves? Actually, consider depth d: each node at depth d corresponds to a set of 2^{n-d} leaves. Marked leaves can be distributed across these nodes. The number of nodes at depth d that contain at least one marked leaf is at most the number of distinct groups: you could have at most one marked leaf per node, but if you have more than 2^d marked leaves, you could potentially fill all 2^d nodes. So the maximum is min(2^d, k). However, for d close to n, 2^d might be huge, but k is only 1e5, so min(2^d, k) = k when 2^d >= k. For example, at depth n (leaves), each leaf is a node. There are 2^n leaves, but at most k of them are marked, so number of leaf nodes with avengers is at most k. At depth n-1, there are 2^{n-1} nodes. Could each of them contain at least one marked leaf? Yes, if we have at least 2^{n-1} marked leaves, but k is only up to 1e5, while 2^{n-1} could be huge for n up to 30. So at depth n-1, maximum number is limited by k (since you need at least one marked leaf per node, so you need at least 2^{n-1} marked leaves to fill all, but k is small, so it's at most k). So overall, total distinct nodes across all depths is at most sum_{d=0}^{n} min(2^d, k). For k up to 1e5, n up to 30, this sum is O(k * n) because for d where 2^d <= k, min is 2^d, summing to at most 2k (since sum of powers up to when 2^d <= k is less than 2k). For d where 2^d > k, min = k, and there are at most n - log2(k) such depths, each contributing k. So total O(k * log (2^n / k) + k). More precisely, sum_{d=0}^{n} min(2^d, k) = (2^{m+1} - 1) + (n - m) * k, where m = floor(log2(k)) (so that 2^m <= k < 2^{m+1}). The first part is about 2k, second part is (n - m) * k. Since n max 30, m at least 0, worst-case when k is small, m is small, n - m ~ 30, so bound ~ 30k. When k is large (e.g., 1e5, log2(1e5) ~ 16.6, so m=16, 2^17=131072 >1e5, so m=16? Actually 2^16=65536, 2^17=131072; k=100000, so 2^16 <= k < 2^17, so m=16. Then first part sum_{d=0}^{16} 2^d = 2^{17}-1 = 131071, which is ~1.31e5 > k? But we capped min(2^d, k). For d up to 16, 2^d <= 65536? Actually 2^16=65536 <= k, so min=2^d. Sum_{d=0}^{16} 2^d = 131071, which is about 1.31e5, which is about 1.31k. Then for d from 17 to 30 (14 depths) min = k, so adds 14k. Total ~15.3k ≈ 1.53 million. So total visited nodes could be up to about 1.5 million when k=100k, n=30. That's acceptable. Actually we must also consider nodes where count=0? But we don't recursively explore empty segments because we treat them as base case returning A. However, to compute the cost for a node, we need to consider both children only if we choose to split. But our recursion will explore both children only if we compute cost_split? In the algorithm above, we always compute both children recursively even if count=0? Actually, we only recursively call f for children if we need to compute cost_split. But we compute cost_split to compare with cost_burn. To compute cost_split, we need to compute the costs of the two children. That requires descending into them. But if a child segment has no avengers, its cost will be A (we can compute that without further recursion). However, we still need to call f on that child to get its cost. That's fine; we should still explore child segments that are empty? But if a child is empty, its cost is just A, and we would not further split it (since splitting would be worse). So we can directly return A without recursion. So for empty child, we don't need to go deeper. So the recursion will only go into nodes that have at least one avenger? Actually, we will call f on the child regardless of whether it has avengers. But if we call f with count=0, it returns A immediately without recursion. So the call overhead is there but no further recursion. So total number of function calls is number of nodes we actually compute: includes those with count=0 that are children of nodes that we explore. But how many such empty nodes might be explored? When we split a node that has some avengers, we will examine both halves. If a half has no avengers, it's a leaf call. So each node that has avengers could spawn two children, even if one is empty. So the number of empty nodes visited is at most the number of non-empty nodes (since each non-empty node could have two children, but only one of them might be empty). But worst-case: a node with many avengers could have both children non-empty. The total number of nodes visited (including empty) is bounded by roughly twice the number of non-empty nodes? Actually, in a binary tree recursion that only splits when needed, the number of calls is essentially the number of nodes in the recursion tree that we need to compute. In our algorithm, we compute f for every node that is either the root or is a child of a node that we decided to compute split cost. But we compute split cost for every node that we visit? Actually, our algorithm visits a node, counts avengers. If count > 0 and length > 1, we need to compute cost_split (the sum of children's costs) to compare with cost_burn. That requires computing the children's costs, so we will recursively call f on both children regardless. Thus the recursion tree we explore is exactly the set of all nodes that are reachable from the root by following paths as long as the node is not a leaf (length=1) or empty? Actually, we will call f on a node if it is either root or called by its parent. The parent always calls both children if it has length >=2 and count>0. So the recursion tree will be a full binary subtree rooted at the root, but pruned at nodes that are empty? Wait: If a node is empty (count=0), we still call f on it (from its parent), and f returns A without further recursion. So the empty node itself is a leaf in the recursion tree (no further calls). So the total number of nodes visited includes all nodes that are either empty or non-empty that are children of non-empty nodes. So the recursion tree is exactly the full binary tree down to the leaves that we need to compute. But that could be huge if we have many non-empty nodes? Actually, if a node is non-empty, we will call both children. So we will explore the entire subtree under that node, regardless of whether the children are empty or not, until we reach segments of length 1 or until we hit empty segments? But we don't stop at empty segments: we call them, they return, but we still need to go deeper? Actually, for an empty node, we return A directly without further splitting. So we do not call its children. So the recursion tree is not "full binary subtree down to leaf length 1" because once a node is empty we stop. For non-empty nodes, we continue splitting until length 1? Possibly we could prune earlier? If a node has avengers, we might still consider splitting. But we will compute both children and so we will explore all descendant nodes until we either reach length 1 (where we can't split) or we reach empty nodes. So essentially, the recursion tree includes all nodes that lie in the union of the paths from the root to each leaf that contains an avenger, plus all siblings along those paths? Let's think: Suppose we have a node with avengers. We split. Both children are called. If the left child has some avengers, we will further split it; if the right child has no avengers, it returns A and we don't go deeper. So we only go deeper into subtrees that contain avengers. However, we also call the empty child but not its children. So the total nodes visited = for each node in the recursion tree, it is either a node that contains avengers (and then we also call its children, so it's internal), or it's an empty node that is a leaf in this recursion tree. The number of empty nodes visited is at most the number of non-empty nodes (since each non-empty node can have up to 2 children, but both could be non-empty). So total nodes visited is O(number of non-empty nodes). And number of non-empty nodes is the number of distinct nodes that contain at least one avenger, which we bounded as O(k * n). So overall O(k * n) nodes visited. That's acceptable.
But we also need to consider that we might also have nodes that contain avengers but it might be beneficial to stop splitting early and burn; our recursion will still compute children to compare. So we must explore them anyway.
Thus algorithm is feasible.
Now we need to be careful about integer overflow: cost = B * count * l. B up to 1e4, count up to k = 1e5, l up to 2^30 ≈ 1e9. So product could be up to 1e4 * 1e5 * 1e9 = 1e18, which fits in 64-bit (max ~9.22e18). So we need to use 64-bit (long long in C++). Also A up to 1e4, but multiplied by possibly many segments? But sum will be within 64-bit.
Edge Cases:
- Duplicate positions: sorting will keep duplicates; count will include all. That's fine.
- When splitting, mid = (l + r) / 2, integer division. Since l and r are integers, length is power of 2, so mid is integer.
Implementation details:
We can implement a recursive function:
using ll = long long;
vector pos; // sorted positions
ll solve(ll l, ll r, int lo, int hi) {
// count = number of positions in [l, r] from indices [lo, hi)
// Actually we need to ensure that positions within [lo, hi) are exactly those in [l, r]. We'll maintain that.
ll len = r - l + 1;
int cnt = hi - lo;
if (cnt == 0) {
return A;
}
ll burn = B * cnt * len;
if (l == r) {
return burn; // length 1, cannot split
}
// split
ll mid = (l + r) / 2;
// find split index: first > mid
int mid_idx = upper_bound(pos.begin() + lo, pos.begin() + hi, mid) - pos.begin();
ll left_cost = solve(l, mid, lo, mid_idx);
ll right_cost = solve(mid+1, r, mid_idx, hi);
ll split_cost = left_cost + right_cost;
return min(burn, split_cost);
}
But note: we need to use A and B from global variables.
Complexities: Each node does a binary search on a subarray of size up to cnt. Upper_bound on vector is O(log(cnt)), but we need to pass iterators: we can compute using pos.begin() + lo, pos.begin() + hi. That's fine.
Potential optimization: Instead of binary searching each time, we could maintain two pointers? But not necessary.
Now we need to consider that we call solve with lo and hi indices that cover exactly positions in [l, r]. But we must ensure that initial call: lo = 0, hi = k (number of positions). That covers all positions, but are they all in [1, 2^n]? Yes.
One nuance: When we split at mid, left interval is [l, mid], right is [mid+1, r]. Since positions are integers, correct.
Now, consider the cost formula: "burn the current base. If it contains no avenger in it, it takes A amount of power, otherwise it takes his B ⋅ n_a ⋅ l amount of power". So for empty segment, cost is A. For non-empty, cost = B * n_a * l. That's what we used.
Now, let's test with examples.
Example 1:
n=2, L=4, k=2, A=1, B=2
positions: 1,3.
Sorted: [1,3].
Root: l=1,r=4, cnt=2, len=4, burn=224=16. split: mid=2.
Left: l=1,r=2, positions? subset from indices: lo=0, hi=2, find mid_idx = upper_bound([1,3],2) => first >2 is index 1 (since pos[0]=1 <=2, pos[1]=3>2). So left: lo=0,mid_idx=1 => cnt=1, burn left: B12=4. Since l<r, split left: mid=1.5? Actually mid = (1+2)/2=1. So left left: l=1,r=1, subset indices lo=0,mid_idx_left? For left child of left: we compute: call solve(1,1,0,mid_idx_left?) Need to compute split inside left: we will call solve(1,1, lo, mid_idx2) and solve(2,2, mid_idx2, mid_idx). Let's compute: In left (1,2) we find mid_idx_left = upper_bound([1], 1) ? Actually the subarray for left is positions indices 0..0? Wait we passed lo=0, mid_idx=1 for left. So inside left, we have pos from index 0 to 0? Actually hi = mid_idx = 1, so subarray indices [0,1) => contains index 0 only, which is pos[0]=1. So cnt=1. mid = (1+2)/2 = 1. Then we compute split index: upper_bound on this subarray (only element 1) for value 1 => returns iterator to first >1; all elements are <=1? Actually pos[0]=1, which is <=1? Upper bound of 1: first >1, since all <=1, it returns hi (1). So mid_idx2 = 1 relative to subarray? But we need the absolute index: we call upper_bound(pos.begin()+0, pos.begin()+1, 1) which returns pos.begin()+1, so mid_idx_abs = 1. Then left of left: solve(1,1, lo=0, hi=1) => hi=1, lo=0 => cnt=1? Actually positions in [1,1] from indices 0 to 1? But pos[0]=1 is inside, and hi=1 means indices 0..0, count=1. So left leaf: burn = B11 = 2. Since l==r, return 2.
Right of left: solve(2,2, lo=1, hi=1) => hi=1, lo=1 => cnt=0 => return A=1. So left split cost = 2+1=3. Compare to left burn=4, so left chooses 3.
Similarly right segment: solve(3,4, lo=1, hi=2) => subarray contains index 1: pos[1]=3. cnt=1, len=2, burn=212=4. Split: mid=3 ( (3+4)/2=3). Compute split index: upper_bound on [3] with value 3 returns index after 3 (since 3 not >3) => returns hi=2? Actually pos.begin()+1, pos.begin()+2, value 3: upper_bound returns first >3, both elements are 3? Only element 3 <=3, so returns pos.begin()+2, so mid_idx_abs = 2. Then left-right: solve(3,3, lo=1, hi=2) => cnt? indices 1..2, but pos[1]=3, count 1? Actually hi=2, lo=1 => cnt=1 => burn = 211=2. Right-right: solve(4,4, lo=2, hi=2) => cnt=0 => A=1. Sum =3, min with burn=4 => choose 3. Root split cost = 3+3=6, min with burn 16 => 6. Correct.
Example 2:
n=3, L=8, k=2, A=1, B=2, positions 1,7.
Let's compute manually: expected output 8.
We'll trust.
Now, we need to ensure that recursion doesn't cause stack overflow: depth up to 30, fine. But number of recursive calls could be up to ~1.5 million, which is okay if recursion depth is only 30, but total call count may be high. However, recursion in C++ may cause overhead for each call (function call overhead). 1.5 million calls is fine. But we need to be careful about using recursion for all nodes: each node corresponds to a call. That's okay.
However, we should note that we might get up to ~2 million calls, which is acceptable.
Implementation: We'll pass lo and hi as integers.
Potential pitfalls:
-
Using 1-indexed positions; we must be consistent.
-
The length L = 1 << n may be large (2^30) fits in 32-bit signed? 2^30 = 1073741824, which fits in 32-bit signed (max 2^31-1 = 2147483647). So int (32-bit) can hold up to 2^30, but for multiplication we need 64-bit.
-
For n=30, 1<<30 is fine in int on typical compilers (int is 32-bit). But to be safe, use long long for l and r.
-
When computing mid = (l + r) / 2, use integer arithmetic; l and r are long long, but fine.
-
We need to pass A and B as global or as parameters.
-
Input reading: k can be up to 1e5, so use vector.
Now, can we do iterative DP? Not needed.
Let's test with edge cases:
-
k = 0: No avengers. Then the whole base is empty. So we can burn the whole base with cost A. Alternatively, splitting would yield 2 halves each empty, each cost A, total 2A, which is worse. So answer is A. Our algorithm: pos sorted empty, initial lo=0, hi=0, cnt=0, returns A. Good.
-
k > 0 but all in same position: e.g., n=30, k=1e5, all at position 1. Then root count = 1e5, len=2^30 ~1e9, burn = B * 1e5 * 1e9 = 1e18. Split: left half [1, 2^29] contains all avengers (since all at 1), right half empty. So we need to compute recursively. Eventually, when we get down to segment length 1 containing that position, we have many avengers at same leaf? Actually they all at position 1. So count per leaf: at leaf 1, count = k. So leaf burn cost = B * k * 1 = Bk. But also we could burn at higher levels. The recursion will produce many nodes: The path from root to leaf 1 includes all nodes that cover position 1. That's n+1 nodes (including root and leaf). For each node on that path, the sibling subtree (the other half) will be empty. So total nodes visited = nodes on path (n+1) plus siblings (each node's other child is empty). So about 2(n+1) nodes, which is small. So algorithm fast.
-
Worst-case: avengers are scattered such that many nodes are non-empty. For example, place avengers at positions that are distinct and spaced to fill many subtrees. The worst-case number of non-empty nodes is sum_{d=0}^{n} min(2^d, k). For k=1e5, n=30, this sum about 1.5 million as computed. So that many function calls. Each call includes an upper_bound on a subarray of size cnt, which is O(log cnt). So total operations maybe 1.5e6 * log(1e5) ~ 1.5e6 * 17 ~ 25.5e6, which is acceptable in C++.
But we need to be careful about recursion depth: we are not recursing deeply on each call; the call stack depth is limited to n=30, but we will have many recursive calls total (since recursion is tree). This is fine.
Potential memory: vector of positions up to 1e5, fine.
Now, we should also consider that for a node with count > 0 and length > 1, we always compute children. But what if count is huge and burn cost is already minimal compared to splitting? We still compute children. That's okay because we need to compare.
Optimization: We could prune if burn cost for current node is already less than or equal to A * (number of subsegments?) Not necessary.
But one nuance: In the empty case, cost is A. But if we split an empty segment, the total cost would be 2A, which is larger. So we always directly return A. That's fine.
But what about the case where a segment has avengers, but it might be cheaper to treat it as empty? No, because burn cost is B * count * l, which is >0, and empty cost is A (which may be smaller if count small? Wait: But the rule says: if it contains no avenger, it takes A; otherwise it takes Bn_al. So you cannot choose the empty cost if there are avengers. So only two options: burn with that formula, or split.
Thus algorithm stands.
Potential issue: The recursion may compute the same subsegment multiple times? No, because it's a tree partition; each subsegment is uniquely defined by its boundaries. So no overlapping.
Now we need to handle large recursion: We can also implement iteratively using stack, but recursion is simpler.
Now, let's double-check correctness: The division step: "divide the base into 2 equal halves and destroy them separately". That means we can recursively destroy each half. So we sum the costs. Our recursion does that.
Now, note that when we split, we are forced to destroy each half separately (i.e., we must apply the optimal strategy to each half). So we compute optimal for each half and sum.
Edge Cases: When the segment length is 2, we can either burn (cost = B * count * 2) or split into two unit segments. For a unit segment, cost = B * count * 1 if contains avengers, else A. So that matches.
Now, we must confirm the cost for empty segments: The statement: "If it contains no avenger in it, it takes A amount of power". So regardless of length, empty segment costs A. That's a constant. So for an empty segment of any length, burning directly costs A. Splitting would lead to two halves, each empty, each cost A, so total 2A, which is worse. So always burn empty segments directly.
Now, we must consider overflow: B * count * l may exceed 2^63-1? B up to 10^4, count up to 10^5, l up to 2^30 ~1.07e9. Product = 10^4 * 10^5 * 1.07e9 = 1.07e18, which is less than 9.22e18, so safe in signed 64-bit. But if n=30 and count=10^5, product is about 1e18, safe. If B=10^4, count=10^5, l=2^30 -> ~1.07e18. Could there be a case where count is larger? k <= 10^5, so max count is 10^5. So product max ~1e18. So 64-bit okay.
Now, implement.
Pseudo:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector pos;
ll A, B;
int n, k;
ll solve(ll l, ll r, int lo, int hi) {
int cnt = hi - lo;
if (cnt == 0) return A;
ll len = r - l + 1;
ll burn = B * cnt * len;
if (l == r) {
return burn;
}
ll mid = (l + r) / 2;
// Find first index > mid
int split = upper_bound(pos.begin() + lo, pos.begin() + hi, mid) - pos.begin();
ll leftCost = solve(l, mid, lo, split);
ll rightCost = solve(mid+1, r, split, hi);
return min(burn, leftCost + rightCost);
}
int main() {
cin >> n >> k >> A >> B;
pos.resize(k);
for (int i = 0; i < k; i++) {
cin >> pos[i];
}
sort(pos.begin(), pos.end());
ll len = 1LL << n;
ll ans = solve(1, len, 0, k);
cout << ans << endl;
return 0;
}
Let's test with examples.
First example: input: 2 2 1 2\n1 3 => ans=6, correct.
Second: 3 2 1 2\n1 7 => let's manually run to verify. n=3, len=8, positions [1,7].
Root: l=1,r=8, cnt=2, len=8, burn=228=32.
split mid=4. upper_bound of 4: positions: 1,7; first >4 is index of 7 => split=1 (0-based index). left: [1,4], indices 0..1 => includes 1 only? Actually hi=split=1, so left has indices [0,1) => only index 0 (pos=1). right: [5,8], indices [1,2) => pos=7.
Now left segment: l=1,r=4, cnt=1, len=4, burn=214=8. split mid=2. upper_bound on subarray [1] for 2 => first >2 -> all ≤2? 1 <=2, so returns hi=1. So split=1. left-left: [1,2], indices [0,1) => count=1? Actually subarray still only index 0, pos=1. Right-left: [3,4], indices [1,1) => empty.
Now left-left: l=1,r=2, cnt=1, len=2, burn=212=4. split mid=1. upper_bound on subarray [1] for 1 => first >1 returns hi=1. left-left-left: [1,1], indices [0,1) => count=1, burn=211=2. left-left-right: [2,2], indices [1,1) => empty, cost A=1. So split cost = 3. min(burn=4, split=3) = 3.
Now right-left (empty): [3,4] cost A=1.
So left segment cost = min(burn=8, split = left-left cost (3) + right-left cost (1) = 4) => 4? Wait compute: left-left cost we computed as 3, right-left cost = 1, sum = 4. burn=8, so left segment cost = 4.
But earlier we computed left-left cost is 3, and right-left cost 1, sum 4, which is less than 8. So left segment chooses split, cost 4.
Now right segment: l=5,r=8, cnt=1, len=4, burn=214=8. split mid=6? (5+8)/2=6 (integer division 6). Actually (5+8)=13/2=6.5 truncated to 6. So mid=6. But careful: We need to split into two halves of equal length: length=4, halves length 2. So left half: [5,6], right half: [7,8]. That matches mid = (5+8)/2 = 6 (since integer division). Yes, left = 5..6, right=7..8. Good.
Now find split index: subarray indices [1,2) containing pos=7. upper_bound on value 6 returns first >6; 7 >6, so returns index 1 (the first element, since pos[1]=7 >6). So split = 1 (relative to subarray? Actually we use global indices: we call upper_bound(pos.begin()+1, pos.begin()+2, 6). pos.begin()+1 points to pos[1]=7, end at pos.begin()+2 (exclusive). The iterator returned will be pos.begin()+1 because 7>6, so split=1. So left child: lo=1, hi=1 => empty. Right child: lo=1, hi=2 => contains pos=7.
Left child empty: cost A=1.
Right child: l=7,r=8, cnt=1, len=2, burn=212=4. split mid=7 ( (7+8)/2=7). For subarray indices [1,2) with pos=7. upper_bound on 7 returns first >7 -> all <=7, so returns hi=2. So split=2. left-right-left: [7,7], lo=1, hi=2? Actually for right child we call solve(7,8,1,2). In that call, mid=7, split index = upper_bound(...,7) = 2. So left half: solve(7,7,1,2) => hi=2, lo=1 => cnt=1? Actually positions in [7,7] from indices 1..2: pos[1]=7, count=1, burn=211=2. Right half: solve(8,8,2,2) => cnt=0 => A=1. So split cost = 3. min(burn=4, split=3) = 3.
Thus right child cost = min(burn=4, split=3) = 3? Wait we are inside right segment: the right child of the root's right segment? Actually we computed right child of the root's right segment is the segment [7,8] which we just computed cost 3. And left child of root's right segment is empty cost 1. So split cost for the root's right segment = 1 + 3 = 4. burn=8, so right segment cost = 4.
Now total root split cost = left segment cost (4) + right segment cost (4) = 8. burn root = 32. So min = 8. matches example. Good.
Now we should consider the case when splitting yields a lower cost than burning, even when count > 0.
Now, about duplicates: Suppose two avengers at same position. Our sorted list will have both entries. When counting, they both count. For leaf of length 1, burn cost = B * 2 * 1 = 2B. Splitting not possible. That's fine. For higher levels, counts aggregate. Works.
Now, we must ensure that the recursion does not cause deep recursion stack issues: depth max 30, fine.
Potential performance issue: For each node, we compute upper_bound on a subarray, which is O(log (hi - lo)). Since hi-lo could be up to k, that's fine. But we might compute the same bound many times. However, we cannot avoid.
Now, verify the bound on number of nodes: In worst-case, k=1e5, n=30, the number of non-empty nodes could be as high as ~1.5e6. So function calls 1.5e6. Each call includes binary search O(log(hi-lo)). That's acceptable.
But we need to be cautious about recursion overhead: Each function call may have overhead (stack frame). 1.5e6 calls might be okay if the recursion is not too deep, but the total number of calls is large; this is not tail recursion and each call returns. In C++, 1.5 million recursive calls (total across the whole recursion tree) is okay if the program's stack depth is not huge; each call uses some stack memory, but they are not all active simultaneously; maximum active calls equals recursion depth ~30. So total number of calls is just runtime, not memory. So fine.
But we must be careful about the recursion overhead when we have many nodes: 1.5 million function calls may be borderline but typically fine in C++ (around 0.1-0.2 micro per call? Actually function call overhead is small; 1.5 million is fine).
Now we need to consider potential improvement: Could there be a scenario where we have more than 2 million nodes? For k=1e5 and n=30, maximum non-empty nodes is sum_{d=0}^{30} min(2^d, 1e5). Let's compute exactly:
2^0 = 1
2^1 = 2
2^2 = 4
2^3 = 8
2^4 = 16
2^5 = 32
2^6 = 64
2^7 = 128
2^8 = 256
2^9 = 512
2^10 = 1024
2^11 = 2048
2^12 = 4096
2^13 = 8192
2^14 = 16384
2^15 = 32768
2^16 = 65536 (<=100000)
2^17 = 131072 (>100000)
...
2^30 huge.
So sum_{d=0}^{16} 2^d = 2^{17} - 1 = 131071.
For d=17 to 30 inclusive: number of levels = 30 - 17 + 1 = 14. Actually check: d from 0 to 30, that's 31 levels. d=0..16 is 17 levels? Let's count: d=0,1,2,...,16 => 17 levels (since 0 to 16 inclusive = 17). Then d=17..30 => 14 levels (since 30-17+1=14). So sum_{d=17}^{30} min(2^d,100000) = 14 * 100000 = 1,400,000. So total distinct non-empty nodes = 131,071 + 1,400,000 = 1,531,071. That's about 1.53 million. Plus empty nodes visited: each non-empty node could have up to 2 children; but some children are non-empty as well, counted in non-empty. The empty nodes are those children of non-empty nodes that themselves are empty. In worst-case, every non-empty node could have two children, both non-empty? That would increase count of non-empty nodes; but we already counted max non-empty nodes. Actually, if a non-empty node has two children, both are non-empty, then the children are counted as non-empty. So empty nodes are only when one child is empty. In the worst-case for total nodes visited, we might have scenario where for each non-empty node, exactly one child is empty. That would add an empty node for each non-empty node. But can we have that many empty nodes simultaneously? Possibly, but then total nodes visited = non-empty + empty ≤ 2 * non-empty, because each non-empty node can have at most one empty child (if it had two empty children, then the node itself would be empty? Actually if both children are empty, then the node would have count 0, contradicting non-empty. So a non-empty node must have at least one non-empty child. So it can have at most one empty child. So total empty nodes <= number of non-empty nodes. So total nodes visited <= 2 * non-empty ≈ 3 million. That's still okay.
So overall, our algorithm is fine.
Now we need to implement in C++.
Potential pitfalls: Recursion may cause TLE if we do many binary searches? But 3 million * log2(1e5) ~ 3e6 * 17 ~ 51 million comparisons. This might be borderline but likely okay with optimized STL and O2. However, we can micro-optimize: Instead of binary search each time, we can manually maintain two pointers? But easier: we can pass the vector and use upper_bound. But note that upper_bound on a vector segment requires random access iterators, it's binary search. It should be fast.
Alternatively, we can precompute the positions and use a recursive function that doesn't need binary search by splitting the array indices along with the segment. That's exactly what we are doing: we pass lo and hi indices that correspond to the subarray of positions that are in the segment. To split, we need to find the point where positions cross the mid. We could use a while loop to find the split point by scanning from lo until we find a position > mid. But scanning could be O(count) per node, leading to O(total number of avengers visited many times) causing O(k * n^2) maybe. Binary search is better.
But we can also note that since we always split the segment in half, and the positions are sorted, we can find the split by using a separate function that finds the first index > mid within [lo, hi) using binary search. That's what we do.
We could also use an iterative approach: Instead of recursion, we could build a segment tree over the sorted positions? Not needed.
Now, one more nuance: The base length is a perfect power of 2, but is it guaranteed that when we divide, each half is also a power of two? Yes, because length is a power of two, dividing by 2 yields integer and still power of two.
Now, about the input: The positions are from 1 to 2^n inclusive. Good.
Now, double-check corner: If A is very large? For example, A = 1e4, B=1, and many avengers. Then burning empty segments is costly, but empty segments always cost A, regardless of length. So for an empty segment of large length, it might be cheaper to split? But we argued splitting empty segment gives sum of children's costs which are each A if they are also empty. If we split an empty segment of length L, each half is empty, each costs A if burned directly. But could splitting further reduce cost? Suppose we split an empty segment all the way down to leaves, we would have L leaves, each empty, each cost A, total = L * A. Since L >= 2, L * A > A. So splitting empty is always worse. So we don't need to consider splitting empty segments.
What about a segment with avengers? Could splitting be beneficial even if one child is empty? Possibly yes, as we saw.
Now, verify for large values: The answer fits in 64-bit.
Now, test with a small custom case: n=1, length=2, k=1, A=5, B=1, pos=1. Options: burn whole: B12=2. Split: left [1,1] has avenger, cost= B11=1; right [2,2] empty, cost=A=5; total=6. So min=2. Our algorithm: root cnt=1, burn=2, split mid=1, left child cnt=1, burn=1, right empty cost=5 -> split=6, min=2. Good.
Now test n=1, length=2, k=0, A=5, B=1. root cnt=0, return A=5. Splitting not considered. Good.
Now test n=3, length=8, k=8 (all positions occupied), A=1, B=1. We can compute: If we burn whole: B88=64. If we split into two length 4 each, each contains 4 avengers, burn each: 2 * (144)=216=32. Further split: each length 2 with 2 avengers: cost 122=4 each, total for four such segments = 16. Further split to length 1 each: each cost 11*1=1, total 8. But there is also empty option? none. So minimal would be 8? But wait, we can also mix: maybe split some and burn some. The recursive algorithm will compute min. Let's manually see: For length 1 leaf, cost=1. For length 2 segment with 2 avengers, option: burn=4, split=1+1=2, so min=2. For length 4 segment with 4 avengers, burn=16, split = cost of two length2 segments = 2+2=4, so min=4. For root length 8 with 8 avengers, burn=64, split = 4+4=8, so min=8. So answer = 8. Our algorithm should get 8.
Now, test duplicates: n=2, length=4, k=3, A=5, B=1, positions = [1,1,1] (3 avengers at position 1). Then leaf 1 has count=3, cost=131=3. leaf 2,3,4 empty cost each A=5 if burned directly. But we may choose to burn larger segments. Let's compute optimal: root: len=4, count=3, burn=134=12. Split: left [1,2] count=3, right [3,4] count=0.
- Right: empty, cost A=5.
- Left: len=2, count=3, burn=132=6. split: left-left [1,1] count=3, burn=3; left-right [2,2] empty cost=5; split total=8; min left = min(6,8)=6. So left cost=6. So root split total = 6+5=11, min with burn 12 => 11. So answer 11.
Our algorithm: fine.
Now, consider A large: n=2, length=4, k=1, A=100, B=1, pos=1. Burn root =114=4. Split: left [1,2] count=1, burn left =112=2; right empty cost=100; total=102. So min=4. Good.
Now, consider B small: n=2, length=4, k=1, A=1, B=1000, pos=1. Burn root = 100014=4000. Split: left cost? left: [1,2], count=1, burn=100012=2000; split further: leaf 1 cost=1000, leaf 2 empty cost=1 -> total=1001; left min = min(2000,1001)=1001. Right empty cost=1. So split total=1002. So min=1002. Our algorithm yields 1002.
Now, about integer overflow when computing burn: B * cnt * len. B and cnt are ints, len is long long. Product might be computed as int * int * long long; we must ensure to cast to long long: multiply B (ll) by cnt and then by len. In C++, if we do B * cnt * len, it will multiply left to right: B and cnt are ints, product may overflow if >2^31-1. B max 1e4, cnt max 1e5, product = 1e9, which fits in int32? Actually 1e9 < 2^31-1 (~2.147e9), so it's safe. But if B=1e4, cnt=1e5 => 1e9, fine. However, if B=1e4, cnt=1e5, product=1e9, safe. But if B=1e4, cnt=2e5? cnt max is 1e5, so safe. So Bcnt is within int32. Then times len (up to 1e9) could be 1e9 * 1e9 = 1e18, which is >2^31, so we need the intermediate to be promoted to 64-bit. In C++, when you multiply int * int, result is int; then that int is multiplied by long long, so the int product is already computed as int, could overflow if Bcnt > 2^31? But we argued Bcnt <= 1e9 < 2^31, safe. However, if A and B were up to 10^4, cnt up to 10^5, product <= 10^9, safe. But if B=10^4, cnt=10^5, product=10^9, which fits in signed int (max 2.147e9). So safe. But if n=30, len up to 1,073,741,824, Bcnt=1e9 * 1,073,741,824 = 1.073e18, which fits in 64-bit. So we are safe as long as Bcnt doesn't overflow int. Since int is typically 32-bit signed, max is about 2.147e9. Bcnt max 1e4 * 1e5 = 1e9, safe. So we can compute as (ll)B * cnt * len, but B*cnt may be computed as int and then cast to ll; it's fine. To be safe, we can cast: (ll)B * cnt * len.
Now, implementation details: We'll use long long for positions too? They are up to 2^30, fit in 32-bit signed. But to avoid issues, we can store in vector? But then when we do upper_bound with long long? mid is long long, but positions are int. That's okay because int can be promoted.
Better to use vector to be safe.
Now, final code.
But wait, there is a nuance: In the recursion, we pass lo and hi as ints, but hi - lo can be up to 1e5, which fits int. However, the total number of recursive calls might be up to 3 million, which could cause stack overflow if recursion depth is 30? Actually recursion depth is small, but total number of calls is not the depth; the recursion function calls itself for children and then returns. The call stack at any point is depth <= 30. So the stack memory is fine. However, the number of function calls (invocations) is high, but that's runtime, not stack depth. So no stack overflow.
Now, we should test with worst-case input to ensure it runs within time. But as this is a typical problem (Codeforces problem "Avengers" maybe?), this solution is standard.
Now, one more nuance: The problem states "each position can be occupied by many avengers, but one avenger can occupy only one position." That suggests duplicates allowed. So we count duplicates.
Now, let's consider the possibility that A could be zero? Input constraints: 1 ≤ A,B ≤ 10^4, so positive.
Now, could the cost be zero? Only if both A and B zero, but they are at least 1, so no.
Now, final code.
But we should also consider the scenario where splitting leads to infinite recursion if we don't check length >=2? We check l == r as base case, so fine.
Now, about the binary search: We need to compute upper_bound on the subarray. Since we are using global indices, we need to be careful that the subarray indices [lo, hi) are within range. For non-empty nodes, hi > lo.
Now, we could also pass the vector and indices by reference, but okay.
Now, let's implement.
Potential micro-optimization: Instead of using upper_bound each time, we can use lower_bound for the first > mid, same.
Now, I'll write the code.
But before finalizing, test with a custom random test to ensure correctness.
I'll write a quick mental test: n=2, length=4, k=4, all positions: [1,2,3,4], A=1, B=1. The optimal is to split fully: each leaf cost 1, total 4. Let's see algorithm: root cnt=4, burn=144=16, split: left [1,2] cnt=2, right [3,4] cnt=2.
- left: len=2, burn=122=4, split: left-left [1,1] cnt=1 burn=1, left-right [2,2] cnt=1 burn=1, split cost=2, min=2.
- right similarly: 2.
So root split cost = 4, min=4. Good.
Now test n=3, length=8, k=4, positions: 1,2,7,8, A=1, B=1. Possibly optimal: split root into [1-4] and [5-8]. Left: [1-4] has avengers at 1,2 => count=2. left-left [1-2] has both, burn=122=4, split: each leaf 111=1 each => total 2, so left-left min = 2; left-right [3-4] empty cost=1; total left = 3? Actually left segment [1-4]: we have left-left cost=2, left-right cost=1 => split=3; burn left =124=8; so left cost=3. Right segment [5-8] has avengers at 7,8 => similarly cost=3. Total =6. Burn root: 148=32, min=6. Could there be a better strategy? Maybe split differently? The only allowed splits are equal halves. So 6 seems correct. Our algorithm yields.
Now, what about scenario where A is smaller than B? For empty segments, cost A; for non-empty, cost B * count * l. If A is very small, maybe it's better to split a non-empty segment into pieces that have many empty subsegments, because empty subsegments cost only A, which could be lower than the burn cost of the larger segment. Our recursion captures that.
Now, we should also consider if there is any scenario where burning a non-empty segment might be cheaper than splitting even when splitting yields lower cost in terms of B * count * l, because of the empty parts? That's captured by comparing.
Now, check for integer overflow in multiplication of B * cnt * len: B up to 1e4, cnt up to 1e5, len up to 2^30. So product up to 1e9 * 1e9 = 1e18, safe.
Now, I think solution is correct.
We'll produce final answer.
Now, just to be thorough, let's consider the possibility of using iterative DP with map, but recursion fine.
Now, before final, test with n=30, k=100000, all positions distinct and spread evenly. The recursion will explore many nodes, but should run within time (maybe a few hundred ms in C++? Possibly 1 second? But Codeforces time limits typically 1-2 seconds, this is a known problem (CF 1111C? Actually there's a problem "Thanos and the Avengers" maybe? I recall a problem "Avengers" in Codeforces Round #? It's Codeforces problem 1111C? Let's check: There is a problem "1111C - Creative Snap" which is very similar: It's about Thanos destroying the Avengers base. Exactly that problem: "Creative Snap". The description matches: n, k, A, B, positions. So it's Codeforces 1111C. The constraints: n up to 30, k up to 100000, A,B up to 10000. The solution is exactly this recursion with counting using binary search. Many solutions exist.
In that problem, the time limit is 1 second, and solutions using recursion with binary search on sorted list pass. So our approach is fine.
But note: In that problem, there is a nuance: The base length is 2^n, and positions are given. There's no requirement that avengers are unique. So duplicates allowed. So we are good.
Now, just a small nuance: In the problem statement here, it says "each position can be occupied by many avengers, but one avenger can occupy only one position." That is the same as duplicates allowed.
Thus final answer.
Now, I'll write the code accordingly.
But wait, there's an alternative: We could precompute prefix sums to count avengers quickly? Since we have sorted positions, binary search is fine.
Now, we might need to handle large recursion: The function will be called many times. To avoid overhead, we could implement iteratively with a stack, but not necessary.
One more micro-optimization: Use references for pos to avoid copying, but it's global.
Now, write code:
#include
#include
#include
using namespace std;
typedef long long ll;
vector pos;
ll A, B;
ll solve(ll l, ll r, int lo, int hi) {
int cnt = hi - lo;
if (cnt == 0) {
return A;
}
ll len = r - l + 1;
ll burn = B * cnt * len;
if (l == r) {
return burn;
}
ll mid = (l + r) / 2;
// find first index > mid
int split = upper_bound(pos.begin() + lo, pos.begin() + hi, mid) - pos.begin();
ll leftCost = solve(l, mid, lo, split);
ll rightCost = solve(mid + 1, r, split, hi);
return min(burn, leftCost + rightCost);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, k;
cin >> n >> k >> A >> B;
pos.resize(k);
for (int i = 0; i < k; ++i) {
cin >> pos[i];
}
sort(pos.begin(), pos.end());
ll len = 1LL << n;
ll ans = solve(1, len, 0, k);
cout << ans << endl;
return 0;
}
That should work.
Now, check for possible off-by-one: In solve, we use r - l + 1 for length. For leaf, l == r, len=1. Good.
Now, about the split index: We use upper_bound to get first > mid. That ensures left contains positions <= mid, right contains > mid. Since intervals: left = [l, mid], right = [mid+1, r]. This matches.
Now, consider if mid is integer and positions are integer. Works.
Now, test with duplicate positions that are exactly mid: For left half, we include positions <= mid. That's correct because left half includes mid. For right half, positions > mid. Good.
Now, edge: If positions include mid and mid+1, they are correctly separated.
Now, time complexity: As argued.
Now, we should ensure we use long long for len and mid because l and r can be up to 2^30, which fits in int32? Actually 2^30 = 1073741824, which is within signed 32-bit max 2,147,483,647. So int can hold it. But we used long long for safety and for multiplication with B*cnt. It's fine.
Now, final answer.
But I recall a nuance: In the problem "Creative Snap", the cost for burning a segment with no avengers is A, but for non-empty it's B * number_of_avengers * length. However, there is also a possibility that A could be larger than B*something? Not relevant.
Now, also note that for empty segments, we might consider splitting if A is zero? But A >=1, so no.
Thus code is ready.
One more thing: In some solutions, they define the recursion with length and start, but that's similar.
Now, finalize. ```cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector pos; // sorted positions of all avengers
ll A, B; // constants from input
// Recursively compute minimal power for segment [l, r] (inclusive)
// lo, hi are indices in pos such that positions in [lo, hi) lie inside [l, r]
ll solve(ll l, ll r, int lo, int hi) {
int cnt = hi - lo; // number of avengers in the segment
if (cnt == 0) return A; // empty segment: burn directly
ll len = r - l + 1; // length of the segment
ll burn = B * cnt * len; // cost if we burn it now
if (l == r) return burn; // cannot split further
ll mid = (l + r) / 2;
// Find the first position > mid within the current subarray
int split = upper_bound(pos.begin() + lo, pos.begin() + hi, mid) - pos.begin();
ll leftCost = solve(l, mid, lo, split);
ll rightCost = solve(mid + 1, r, split, hi);
return min(burn, leftCost + rightCost);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, k;
cin >> n >> k >> A >> B;
pos.resize(k);
for (int i = 0; i < k; ++i) {
cin >> pos[i];
}
sort(pos.begin(), pos.end());
ll len = 1LL << n;
ll ans = solve(1, len, 0, k);
cout << ans << '\n';
return 0;
}