We need to parse problem: Given a segment [0, n] of the street with positions integer from 0 to n-1 to possibly install lamps. Some positions are blocked: we cannot place a lamp there. We can buy lamps of exactly one type (power l, where l in [1, k]) and we can buy as many as we want (infinite). Each lamp placed at a non-blocked position x (x must be integer in [0, n-1]? Actually positions are from 0 to n-1 inclusive? They said positions from 0 to n-1 on OX axis. And lamps placed at position x (must be integer and not blocked). The lamp illuminates [x, x + l] (including x to x+l). We need to illuminate entire segment [0, n] (meaning every point in that closed interval should be covered by at least one lamp' illumination). Overlapping coverage is fine. Lamps can be placed only at positions 0..n-1? Because there are exactly n positions to install lamps, they correspond to integer numbers from 0 to n-1 on OX axis. So yes, only integer positions from 0 to n-1 inclusive are possible installation spots. However, lamps can be placed only on these positions, but they can illuminate beyond n (up to x+l). So the segment [0, n] must be covered. We are allowed to use lamps of exactly one power l (chosen from 1..k). Cost per lamp is a_l. We want minimal total cost (i.e., number of lamps * a_l) to cover [0, n] using only lamps of that power, respecting blocked positions.
We need to compute, for each possible l, whether it's possible to cover [0,n] using lamps of power l (only placed on allowed positions). If possible, compute minimal number of lamps needed (since cost per lamp fixed, minimal number = minimal cost). Then pick smallest cost across l. If none possible, print -1.
Constraints: n up to 10^6, k up to n, m up to n. So we need O(n log n) or O(n) per l? But we have up to k=10^6; we can't do O(n) for each l individually, that would be O(n*k)=10^12. Need smarter approach.
Observe: For a given l, we need to cover segment [0,n] with intervals [x, x+l] where x is in allowed set A (non-blocked positions in [0, n-1]). Each interval length is l. Since intervals are of same length, and left endpoints restricted to allowed positions. This is like covering the line from 0 to n with intervals of fixed length l, placed at positions from a given set S (allowed). The minimal number of intervals to cover [0,n]? This resembles a greedy: we need to cover starting from 0, at each step choose the furthest possible placement such that its left endpoint <= current coverage endpoint? Actually typical interval covering problem: given intervals [s_i, s_i+l] where s_i in S, we need to cover [0,n] minimally. The standard greedy: start at point 0, choose the interval that starts <= current point and extends as far right as possible. Then set current point to the right endpoint of that interval, and repeat. Since all intervals have same length l, the best we can do is always choose the largest s (position) that is allowed and <= current point (since bigger s gives right endpoint s+l, which is larger if s is larger, provided s+l covers beyond current). Actually wait: we need to cover from current point "cover_up_to". At state we have covered [0, cover_up_to]. We need to cover from cover_up_to to n. To extend coverage, we can place a lamp at a position x such that x <= cover_up_to (since its left endpoint must be at or before the uncovered leftmost point? The interval covers [x, x+l]; to cover cover_up_to, we need x <= cover_up_to <= x+l. So condition: x <= cover_up_to <= x+l. But if we want to extend as far as possible, we want the maximum x such that x <= cover_up_to, because that gives the right endpoint x+l. However, we also need to ensure that x is allowed (not blocked). Also, x must be in [0, n-1]. So the greedy: At each step, find the maximal x in S (allowed) with x <= cover_up_to. Then we set cover_up_to = x+l. If such x does not exist, then covering fails for that l. But is this always optimal? In the standard covering with intervals of fixed length, indeed the greedy that picks the farthest starting point that still covers the current point yields minimal number. However, there is a nuance: what if there is a blocked position and we might need to use a position that is less than cover_up_to but yields the same right endpoint? Since length is fixed, the right endpoint is x+l, so maximizing x (subject to x <= cover_up_to) maximizes right endpoint. So yes, greedy optimal.
But careful: The condition also requires that x is within [0, n-1]. Also, when cover_up_to >= n, we are done.
So for each l, we need to simulate this greedy using the set of allowed positions. However, we cannot simulate for each l individually because that would be O(n) per l.
We need to find a way to compute the minimal number of lamps needed for each l efficiently, possibly in O(n) overall or O(n log n). Since k up to n=10^6, we could consider iterating over l from 1 to k, but we need to avoid O(n) per l.
Observation: The greedy process for a given l can be described in terms of jumps: Starting from pos = 0, repeatedly find the largest allowed position <= pos, set pos = that position + l. If no such allowed position, fail.
The set of allowed positions S (non-blocked) is the complement of blocked positions among 0..n-1. Let's denote allowed = all integers from 0 to n-1 except those in B (blocked set). Let m be number of blocked positions. So allowed size = n - m.
But m can be up to n, but okay.
We need to answer for many l: the minimal number of steps (lamps) required.
One potential approach: For each l, the greedy can be seen as walking on allowed positions with step length l? Actually if we precompute an array nextAllowed[position] = the largest allowed position <= position. Then the greedy becomes: pos = 0, while pos < n: let x = nextAllowed[pos]; if x == -inf? Actually if nextAllowed[pos] is defined? But we need the largest allowed <= pos. If no such allowed position, then fail. Then pos = x + l; count++. This will give the answer for each l in O(number of steps) which could be up to n/l? Since each step advances at least l (actually might be more if x+l > pos+l because x could be less than pos, but at most x+l >= l? Actually pos is the current coverage endpoint, which might be > x+l? Wait: We set pos = x+l. Since x <= previous pos, x+l <= previous pos + l. But previous pos could be larger than x? Actually previous pos was coverage endpoint. Then after stepping, new coverage = x+l. So we advance by at least? Not necessarily: previous pos to new coverage can be less than l? For example, if previous pos was far right and we used a lamp at x which is much left of previous pos, then x+l may be less than previous pos? That can't happen because condition is x <= previous pos <= x+l. So previous pos <= x+l, so new coverage is at least previous pos (if previous pos <= x+l). Actually we set new pos = x+l, which is >= previous pos because previous pos <= x+l. So coverage is non-decreasing. The increment is new pos - previous pos = (x+l) - prev_pos. Since x <= prev_pos, that increment <= l, but could be 0 if prev_pos = x+l? That would be if prev_pos already at max. But generally, we hope it advances by some amount. In worst-case scenario, if we always pick x = prev_pos (if allowed), then we advance by exactly l. But if allowed positions are sparse, we might need to pick x smaller than prev_pos, so advance less. So number of steps can be as high as O(n) for small l (if l=1 and all positions allowed, steps = n? Actually covering [0,n] with intervals length 1: if allowed all positions, greedy: start pos=0, x=0 (largest allowed <=0) -> new pos=1; then pos=1, x=1 -> new pos=2; etc. Steps = n (since need to cover each unit). So steps = ceil(n/l) worst-case? Not exactly, but about n/l.
So per l, steps could be up to O(n) when l=1. That is O(n) per l, summing over l from 1 to k gives O(n*k) huge.
But maybe we can compute for all l simultaneously using some DP? Consider that for each allowed position i, it can "jump" to i+l, where l is variable. But l varies.
We need to answer: For each l, minimal number of lamps = minimal number of steps to reach >= n by repeatedly: from current pos, choose max allowed <= pos, then go to that allowed + l.
Alternate perspective: Let's define an array f[l] = minimal number of lamps needed? But perhaps we can compute for each l whether it's possible and the count by scanning from left to right, using a concept of "next allowed to the right"? Actually the greedy uses the largest allowed <= current pos. So it's like we have a "coverage frontier" that moves forward. The condition for success is that there is no gap larger than l between allowed positions? Wait, think.
Suppose we have allowed positions set S. We want to cover [0,n] with intervals of length l starting at allowed points. We need to cover point 0. The leftmost covered point is 0, so we need an allowed position x0 such that x0 <= 0 <= x0+l, i.e., x0 <= 0. Since positions are non-negative, the only possible x0 is 0, if 0 is allowed. Actually x0 can be 0? x must be >=0. So x0 must be 0. Because if x0 > 0, then x0 > 0 and x0 <= 0 condition fails. So to cover 0, we must have a lamp at position 0 (since lamp at position 0 illuminates [0, l]). But wait, could a lamp placed at negative position? No, only positions 0..n-1 are allowed. So first lamp must be at 0. So condition: position 0 must be allowed. If blocked, then impossible for any l? Actually is it always necessary? Consider if l>0, can a lamp at position 1 cover 0? It illuminates [1,1+l], which does not include 0. So 0 not covered. Lamp at position -1 not allowed. So indeed, to cover point 0, we must place a lamp at 0. So the first lamp must be at position 0. However, the problem statement says: "the post lamp shop provides an infinite amount of lamps of each type from power 1 to power k. Though each customer is only allowed to order post lamps of exactly one type." It doesn't specify that we must place lamps only at allowed positions? Yes, "some positions are blocked and no post lamp can be placed there." So we cannot place a lamp at blocked positions. So if 0 is blocked, then impossible to cover 0, thus answer is -1 for all l. Actually check examples: Input 5 1 5; blocked: 0; output -1. Yes, matches. So indeed, if position 0 is blocked, answer -1 immediately.
Thus assume 0 is allowed (otherwise answer -1). So first lamp always at 0.
Now after placing lamp at 0, we cover [0, l]. So coverage up to l. Next, we need to cover from l to n. Greedy: current coverage end = cur = l. Find largest allowed position x <= cur. If such x exists, then place lamp at x, extends coverage to x+l. Continue.
Now, to check possibility for a given l, we need to ensure that for each step, the largest allowed <= cur exists, and that eventually cur >= n. This is like ensuring that the gaps between allowed positions are not too large relative to l.
If we think about the gaps: Let's define the allowed positions sorted: p0=0, p1, p2, ..., p_{t-1} (where t = n - m). Because 0 is allowed (if not, impossible). The greedy essentially uses the allowed positions as stepping stones: we start at p0=0. At each step, we have current coverage endpoint cur. The next lamp we place will be at the largest allowed position <= cur, say p_i. Then cur becomes p_i + l. So cur advances to at most p_i + l. Since p_i is the largest allowed <= cur, any allowed positions greater than p_i but <= cur? Actually p_i is the largest allowed <= cur, so the next allowed after p_i is p_{i+1} which > cur (or may not exist). So we skip to p_i. Then cur becomes p_i + l. For the process to not get stuck, we need that at each step, the distance between cur and the next allowed position after p_i (i.e., p_{i+1}) is not larger than l? Wait, not exactly.
We can think of it as: we maintain a pointer i to the current allowed position used. Starting with i=0, cur = p0 + l. Then, while cur < n, we need to find next index j such that p_j is the largest allowed <= cur. That j will be at least i (since p_i <= cur because p_i + l? Actually cur = p_i + l >= p_i, so p_i <= cur, but could be that p_{i+1} <= cur if cur advanced enough). So we want to jump to the furthest allowed that is <= cur. That is essentially moving i to the largest index such that p_i <= cur. Then update cur = p_i + l. So we are moving forward through allowed positions in a monotonic way: i increases.
This is reminiscent of a "next jump" process similar to computing the minimal number of jumps to cover array using intervals of fixed length, but here the jump length is l plus offset relative to p_i.
Let’s define for each allowed position p, the reach = p + l. Starting from p0=0, we can jump to the farthest allowed position that is <= reach? Actually careful: Greedy: we are at coverage cur (the farthest point covered). The next lamp placed at the largest allowed position <= cur. That gives new cur = that allowed + l. So from cur, we consider allowed positions up to cur, and we pick the max. So it's like we have a function f(x) = max{ p in S | p <= x } (if exists), then cur_{new} = f(cur) + l. So the process is deterministic given l. We can compute for each l the number of steps until cur >= n.
Now, the set S is known (allowed positions). For each possible cur (which can be integer? Actually cur can be non-integer? But illumination is continuous, but coverage endpoints are rational? Lamps cover [x, x+l] with x integer, l integer. So cur will be integer? Starting cur = 0 + l = l (integer). Then cur is always integer plus l? Actually f(cur) returns an integer allowed position. Then cur_new = f(cur) + l, which is integer + l = integer? Since l integer, yes cur is integer always. So cur are integer values. Also, cur can range from 0 to n (or beyond). Since n is integer, we consider integer points.
Thus we need to process many l's.
Observation: The greedy algorithm's steps only depend on the set S and l. For a fixed S, we can precompute something like "next allowed position to the right" or "largest allowed <= x" easily via an array of size n. But we still would need to simulate for each l separately, which could be expensive.
Perhaps we can derive condition for feasibility directly: For l to work, there must be no gap between consecutive allowed positions that exceeds l? But let's test: Suppose allowed positions: 0, 5. n=10. l=4: Greedy: first lamp at 0 covers [0,4]. cur=4. Largest allowed <=4 is 0? Actually 5 >4, so only 0 is allowed <=4. So f(4)=0. Then new cur = 0+4=4 again! That's stuck, because we cannot progress. So we need to have an allowed position within cur (i.e., some allowed position x such that x <= cur and x > previous used? Actually we need f(cur) to be > previous used? In this case f(4)=0, same as before, so cur doesn't advance. So l must be such that for any cur we encounter, f(cur) is advancing. The key is: we need that after placing a lamp at p_i, the next coverage endpoint p_i + l must be at least the next allowed position p_{i+1} (or else the next step's f(cur) will be p_i again, causing infinite loop). Because after we use lamp at p_i, cur = p_i + l. For the process to move forward, we need that there exists an allowed position <= cur that is greater than p_i, i.e., p_{i+1} <= cur. Because if p_{i+1} > cur, then the largest allowed <= cur is still p_i, and we will place another lamp at p_i? But we already placed a lamp at p_i, but we could place multiple lamps at the same position? The problem doesn't forbid placing more than one lamp at the same position? Usually we can place multiple lamps, but is there any restriction? It says "install some post lamps" but doesn't say that each position can have at most one lamp. Typically, you can install multiple lamps at same position, but is that allowed? Usually in such problems, you can place any number of lamps, even at the same spot. But let's examine: If we place another lamp at the same position p_i, would that help? It would cover [p_i, p_i+l] again, which doesn't extend coverage. So it would be useless. So we wouldn't choose it in the greedy because it wouldn't advance cur. But the greedy algorithm, if the only allowed position <= cur is p_i, then it would place a lamp there, but that doesn't advance cur, so we'd never reach n. So that l would be impossible. So condition: After using p_i, we must have p_i + l >= p_{i+1} (the next allowed position). If not, we get stuck.
But is that condition sufficient? Let's check: Starting from p0=0, we need p0 + l >= p1. Then after placing lamp at p1? Actually after p0, cur = p0 + l. Then we choose the largest allowed <= cur. Since p1 <= cur (by condition), that largest allowed is p1 (assuming p1 <= cur and p2 > cur maybe). So we place lamp at p1, cur becomes p1 + l. Then we need p1 + l >= p2 to be able to reach p2. So in general, we need for each consecutive allowed position, p_i + l >= p_{i+1}. However, is it necessary to use every allowed position? Possibly we might skip some allowed positions if l is large enough. For example, allowed positions: 0, 2, 3, ... With l=5, after p0=0, cur=5. The largest allowed <=5 could be 3 (if 3 is allowed and maybe 4 is blocked), not necessarily p1=2. So we might jump over some allowed positions. So the necessary condition for progress is that the maximum allowed position within cur is > previous used. This means that the gaps between successive allowed positions that are "selected" may be larger than 1 step. So condition should be: For each "selected" position, its coverage must reach at least the next selected position? But we can formalize.
Better: The greedy algorithm described earlier (choosing largest allowed <= cur) will succeed if and only if starting from cur = l, then iteratively cur = max{ p in S : p <= cur } + l eventually reaches >= n. This is equivalent to: Let’s define a function next(x) = max{ p in S : p <= x }. Then process x_{0}=0? Actually start: after first lamp at 0, cur = l. Then x_{t+1} = next(x_t) + l? Wait careful: Let cur_t be coverage after t lamps (cur_0 = 0? Actually before placing any lamps, coverage is 0? But we always place lamp at 0 first? We assumed 0 is allowed. So after 1st lamp, coverage = 0+l = l. So we can think we start with cur = 0 (coverage up to 0). Then while cur < n: let p = next(cur); if p is None (i.e., no allowed <= cur) then fail; if p + l <= cur then no progress? Actually if p <= cur, then p + l >= cur? Not necessarily: if p + l <= cur, then placing lamp at p does not increase coverage; it would stay same or less? But if p + l <= cur, then new coverage = p+l, which is <= cur. So that would not advance. But does that happen? Since p <= cur, p+l might be > cur if l > 0 and p is close to cur? Actually p <= cur, so p + l <= cur + l. It could be <= cur if l is small and p is much less than cur. Example: cur=10, l=1, p=0, then p+l=1 <=10. That would not advance. But would the greedy ever be in such situation? At start cur=0, we force p=0? Actually at start, before any lamp, we need to cover point 0. So we must place lamp at 0. That's a special case because cur=0, p=0, p+l = l >= 0. So that advances. After that, cur becomes l. For subsequent steps, if cur is some value, p is the largest allowed <= cur. Since p <= cur, p+l could be less than cur if p is much smaller than cur and l is small. But if p is the largest allowed <= cur, then there is no allowed between p and cur. So if p+l < cur, then placing lamp at p does not extend coverage beyond cur; so we are stuck (can't progress). So for progress we need p+l > cur, i.e., p+l >= cur+1? Actually we need new coverage > cur to eventually reach n. So condition p+l > cur. Since p <= cur, we need l > cur - p. So cur - p < l. That is, the distance from the largest allowed left of cur to cur must be less than l. If that fails, impossible.
Thus, the greedy condition is: At each step, let p = max allowed <= cur, we require p + l > cur, and then set cur = p+l. Then proceed.
This is reminiscent of a coverage condition: The gaps between allowed positions must be less than l, but also relative to the process.
Consider the sorted allowed positions: p_0=0, p_1, p_2, ..., p_{t-1} (with t = number of allowed). Let’s also consider n maybe? The street goes to n; we don't need to place lamp at n because positions only up to n-1, but we may need coverage beyond last allowed? The last lamp placed at some p_i covers up to p_i + l. We need p_i + l >= n.
Now, what's a necessary and sufficient condition for l to work? It seems that if we run the greedy, it will succeed if and only if for every consecutive allowed positions that are "selected" (i.e., the positions we actually place lamps), the distance from that selected position to the next selected position is less than l? But maybe we can characterize via a concept of "max gap between allowed positions" but with an adjustment for the starting point 0.
Let's try to think about the process in terms of jumping between allowed positions. In the greedy, we start at p_0=0. Then we look for the furthest allowed position that is within distance l from the current coverage? Actually current coverage is cur = p_i + l after placing at p_i. The next lamp placed will be at the largest allowed <= cur. That is effectively the furthest allowed that is reachable from the current lamp's coverage without a gap? The condition to move from p_i to p_j (j>i) is that p_j <= p_i + l, and also that there is no allowed between p_j and p_i + l that is larger? Actually the greedy picks the largest allowed <= p_i + l, which will be some p_j. So we jump from p_i to p_j, where p_j is the furthest allowed not exceeding p_i + l. So the process selects a subsequence of allowed positions: start at p_0, then p_{j1} where j1 = max{ j | p_j <= p_0 + l }, then p_{j2} = max{ j | p_j <= p_{j1} + l }, etc.
Thus, the condition for success is that this process eventually reaches a p_k such that p_k + l >= n.
Now, can we precompute for each allowed position its "jump target" for each l? That seems heavy.
Alternative viewpoint: The minimal number of lamps needed for power l equals the minimal number of intervals of length l placed at allowed positions to cover [0, n]. This is a covering problem with restricted left endpoints. This can also be seen as: we want to find a sequence of allowed positions x_1=0, x_2, ..., x_s such that x_{i+1} <= x_i + l (to ensure coverage continuity? Actually to ensure coverage, we need that each point is covered. The intervals [x_i, x_i+l] must collectively cover [0,n]. A sufficient and necessary condition for a sequence to cover is: x_1 = 0 (to cover start), and for each i, x_{i+1} <= x_i + l (so that there is no gap between intervals), and x_s + l >= n. Additionally, each x_i must be allowed. Moreover, we can always assume that the sequence is increasing and each x_{i+1} is the largest allowed <= x_i + l? That yields minimal number. So indeed, the greedy gives the minimal number.
Thus the problem reduces to: For each l, compute the greedy steps count. Need to do it efficiently for all l.
Observing that the greedy steps count is essentially the number of "jumps" needed when moving from 0 to >= n, where from a position p (allowed) you can jump to any allowed q such that q <= p + l, and you want to maximize q to minimize number of jumps. The number of jumps is like the minimum number of steps in this "graph" where from each allowed p you can go to the furthest allowed within distance l to the right.
Define for each allowed p, let next_l(p) = max { q in S | q <= p + l }. Then the process is: start at 0, then repeatedly p := next_l(p) until p + l >= n (then count lamps = number of steps). The number of steps until coverage is the smallest t such that after t steps, we have a position p_t with p_t + l >= n.
This resembles the "farthest reachable" technique used in jump games. For a fixed set S, we can precompute for each position (or each allowed) the next allowed index in sorted order. But l influences the threshold p + l.
We could potentially compute for each l the answer by simulating the jumps using precomputed "next" pointers that can be queried quickly if we can jump many steps at once? However, the number of steps may be O(n) for small l, but small l have many steps, but there are many l values (up to n). Summing steps across all l may be large.
Alternative: Maybe we can compute the minimal cost across l directly by evaluating some conditions about maximal gaps between blocked positions? Let's try to derive constraints.
If there is a blocked position that creates a large gap, some l may be impossible.
Specifically, consider the blocked positions. Let allowed positions be the complement. The process fails if at some point we have a gap where the distance between consecutive allowed positions exceeds l? Actually we saw an example: allowed: 0 and 5, l=4 fails. Gap between allowed positions is 5 (from 0 to 5). l=4 < 5, fails. What if allowed: 0, 2, 5? l=3: Greedy: start 0, cur=3. Largest allowed <=3 is 2 (since 5 >3). So next lamp at 2, cur=2+3=5. Largest allowed <=5 is 5 (since 5 <=5). So lamp at 5, cur=8, covers n=6? Suppose n=6? Actually n is given, but let's assume n=6. Then cur=8>=6 success. Gap between 0 and 2 is 2 <=3, between 2 and 5 is 3 <=3? Actually 5-2=3 equals l, works. So condition might be: For every consecutive allowed positions, the difference must be <= l? In this case 0 to 2 diff=2 <=3, 2 to 5 diff=3 <=3. But what about allowed: 0, 4, 5, n=6, l=3? Allowed positions: 0,4,5. Gaps: 0-4 diff=4 >3. Greedy: start 0, cur=3. Largest allowed <=3 is 0 (since 4>3). So stuck, fails. So indeed if there is any gap between consecutive allowed positions that exceeds l, then the greedy fails? But is that sufficient? Let's test: allowed: 0, 3, 6, n=7, l=4. Gaps: 0-3=3<=4, 3-6=3<=4. Greedy: start 0, cur=4. Largest allowed <=4 is 3 (since 6>4). Place at 3, cur=3+4=7. Since cur>=7, success. Works.
What about allowed: 0, 2, 5, n=6, l=3? Gaps: 0-2=2<=3, 2-5=3<=3. Works. Allowed: 0, 1, 4, n=5, l=2? Gaps: 0-1=1<=2, 1-4=3>2. So gap of 3 >2. Greedy: start 0, cur=2. Largest allowed <=2 is 1 (since 4>2). Place at 1, cur=1+2=3. Largest allowed <=3 is 1? Actually 4>3, so largest <=3 is 1 again. So stuck at cur=3, cannot reach 4. So fails. So condition: Max gap between consecutive allowed positions must be <= l? But is that also sufficient? Let's test more complicated cases: What if there are blocked positions at start? Already we require 0 allowed. Also need to consider the gap from last allowed position to n? Actually the last lamp placed at p_last covers up to p_last + l. So we need p_last + l >= n. This imposes that the last allowed position must be within l of n? Not exactly: p_last may not be the last allowed; we could use a previous allowed if its l covers n. So we need that there exists some allowed p such that p + l >= n, and that p can be reached via the greedy process. But if the max gap condition holds, does it guarantee that we can reach such a p? Possibly yes: If max gap between consecutive allowed <= l, then the greedy will always advance to the next allowed until it eventually reaches one whose l covers n. However, consider allowed: 0, 3, 5, n=7, l=3. Gaps: 0-3=3<=3, 3-5=2<=3. So max gap =3 <=3. Greedy: start 0, cur=3. Largest allowed <=3 is 3 (since 3 <=3, and next is 5>3). So lamp at 3, cur=6. Largest allowed <=6 is 5 (since 5<=6). Place at 5, cur=8 >=7. Success. Good.
What about allowed: 0, 2, 4, n=5, l=3. Gaps: 0-2=2<=3, 2-4=2<=3. max gap=2. Greedy: start 0, cur=3. Largest allowed <=3 is 2 (since 4>3). Place at 2, cur=5. Since cur>=5, done. Success. Good.
What about allowed: 0, 1, 2, 3,... all allowed? Then max gap=1, any l>=1 works. Indeed.
Now consider scenario where max gap <= l but still fails due to something else? Suppose allowed: 0, 3, 4, n=6, l=3. Gaps: 0-3=3 <=3, 3-4=1 <=3. Greedy: start 0, cur=3. Largest allowed <=3 is 3. Place at 3, cur=6. Since cur>=6, done. Works. So fine.
What about allowed: 0, 2, 3, n=5, l=2. Gaps: 0-2=2<=2, 2-3=1<=2. Greedy: start 0, cur=2. Largest allowed <=2 is 2. Place at 2, cur=4. Largest allowed <=4 is 3 (since 3<=4). Place at 3, cur=5. Done. Steps=3. Works.
Thus it seems that condition "max gap between consecutive allowed positions <= l" might be necessary and sufficient for feasibility, given that 0 is allowed and also that last allowed position + l >= n? Wait, consider allowed: 0, 2, 4, n=7, l=3. Gaps: 0-2=2<=3, 2-4=2<=3. So max gap=2 <=3. But does it work? Greedy: start 0, cur=3. Largest allowed <=3 is 2. Place at 2, cur=5. Largest allowed <=5 is 4 (since 4<=5). Place at 4, cur=7 >=7. Success. Good.
Consider allowed: 0, 1, n=5, l=4. Gaps: 0-1=1 <=4, and last allowed is 1. Need 1+4=5 >=5, so works. Greedy: start 0, cur=4, largest allowed <=4 is 1? Actually allowed: 0,1. Largest <=4 is 1. Place at 1, cur=5 >=5. Works.
Consider allowed: 0, 1, n=6, l=4. 1+4=5 <6, so fails because last coverage insufficient even though max gap condition holds. So we also need that there exists some allowed position p such that p + l >= n, and that p is reachable (but if max gap condition holds, then the greedy will eventually reach the last allowed position, so p_last (the largest allowed) will be reached. Then we need p_last + l >= n. So condition: max gap <= l and p_last + l >= n. But what about case where p_last + l < n but some earlier allowed could cover n? If p_last + l < n, then no allowed can cover n because p_last is the largest allowed (positions are only up to n-1). Actually allowed positions are within [0, n-1]. The largest allowed p_max <= n-1. If p_max + l < n, then even if we place a lamp at p_max, it won't reach n. Could we place a lamp at earlier allowed to reach n? For any earlier p <= p_max, p + l <= p_max + l < n, so none can cover n. So indeed we need p_max + l >= n. So condition: the largest allowed position must satisfy p_max + l >= n.
Thus necessary and sufficient conditions for feasibility appear to be:
- 0 is allowed.
- For every consecutive allowed positions (including from 0 to first allowed after 0? Actually sorted allowed positions p0=0, p1, p2, ..., p_t-1), we require p_{i+1} - p_i <= l. (Max gap condition)
- p_{t-1} + l >= n. (Last covers end)
But is condition 2 sufficient to guarantee that greedy works? Let's attempt to prove: Suppose we have allowed positions with gaps <= l, and p_last + l >= n. We'll show that the greedy will succeed. The greedy algorithm: start with cur = 0 (or after first lamp cur = l? Actually after first lamp at 0, cur = l). Since p0=0, we have cur0 = l. For induction, assume after some steps we have cur covering up to some point, and we are at some allowed position index i that was used, such that cur = p_i + l. We need to show that if cur < n, then there exists an allowed position p_j > p_i such that p_j <= cur (maybe?) Actually we need largest allowed <= cur to be > p_i? If p_{i+1} <= p_i + l (gap condition), then p_{i+1} <= cur. Since p_{i+1} is allowed and > p_i, the largest allowed <= cur is at least p_{i+1}. So then we can jump to some p_j >= p_{i+1}. So we advance at least to p_{i+1}. Then new cur = p_j + l >= p_{i+1} + l. Since p_{j} >= p_{i+1}, cur increases. Eventually we will reach p_last because we always move to a larger allowed. Once we reach some p_k where p_k + l >= n, we are done. So yes, condition 2 ensures that we never get stuck because at each step, the next allowed p_{i+1} is within the coverage of the current lamp, so the largest allowed <= cur is at least p_{i+1} > p_i. So we always move forward. Condition 3 ensures final coverage.
Therefore, feasibility for a given l is exactly: 0 is not blocked, the maximum gap between consecutive allowed positions (including from 0 to first allowed? Actually we already have 0 allowed, so gaps are between consecutive allowed positions) is <= l, and also the distance from the last allowed position to n is <= l? Wait, condition 3 is p_last + l >= n, which is equivalent to n - p_last <= l. That is also a gap of sorts: the gap from the last allowed to the end n. So we can treat it as a "terminal gap". However, note that n itself is not a position where we can place a lamp, but we need coverage of [0,n]. So we need that the lamp placed at the last used position covers n. Since the greedy will use p_last if needed, we need p_last + l >= n. So we can define an additional gap: n - p_last. So effectively, we need that all gaps between "critical points" are <= l: gaps between consecutive allowed positions (including the gap from 0 to first allowed after 0? Actually 0 is allowed, so first gap is p1 - p0) and the gap from the last allowed to n. But what about the gap before 0? Not needed.
But is the gap from 0 to first allowed after 0 considered? Since 0 is allowed, the first gap is p1 - 0. So yes.
Thus, let allowed positions sorted: p0=0, p1, p2, ..., p_{t-1}. Define gaps: g_i = p_{i+1} - p_i for i=0..t-2, and also g_end = n - p_{t-1}. Then l is feasible iff max(g_i) <= l (for i from 0 to t-2) and g_end <= l? Wait careful: need p_last + l >= n => n - p_last <= l. That is a condition l >= g_end? Since g_end = n - p_last, yes we need l >= g_end. So overall, we need l >= max( max_gap_between_allowed, g_end ). But also we need 0 allowed (which we have). So feasibility condition: l >= max_gap, where max_gap = maximum over: (differences between consecutive allowed positions) and (n - last_allowed). Actually also consider the case where there are no allowed positions besides 0? If m = n-1, then allowed only 0. Then we have only one allowed p0=0. Then gaps: no consecutive gaps, and g_end = n - 0 = n. So condition: l >= n. That makes sense: with only position 0, we need a lamp of power at least n to cover to n. And we can place multiple lamps at 0? But placing multiple lamps at 0 doesn't help because each covers [0,l], so only one needed if l >= n. So condition l >= n works.
But wait: Is the condition "l >= max_gap" sufficient? Let's test a tricky case: allowed positions: 0, 2, 3, n=5, l=2. Compute gaps: between 0-2:2, 2-3:1, n - last=5-3=2. max_gap=2. So l=2 meets condition. Does it work? Greedy: start at 0, cur=2. Largest allowed <=2 is 2? Actually allowed: 0,2,3. Largest <=2 is 2. So place at 2, cur=4. Largest allowed <=4 is 3 (since 3<=4). Place at 3, cur=5. Done. Works. Good.
What about allowed: 0, 1, 3, n=5, l=2. Gaps: 0-1=1, 1-3=2, n-last=5-3=2 => max_gap=2. Condition l=2 OK. Greedy: start 0, cur=2. Largest allowed <=2 is 1? Because 3 >2. So largest is 1. Place at 1, cur=3. Largest allowed <=3 is 3? 3 <=3, yes. Place at 3, cur=5. Done. Works.
What about allowed: 0, 2, 4, n=6, l=3. Gaps: 0-2=2, 2-4=2, n-last=6-4=2, max_gap=2 <=3. Works. Greedy: 0->cur=3, largest <=3 is 2, cur=5, largest <=5 is 4, cur=7 >=6.
What about allowed: 0, 3, 5, n=7, l=3: gaps: 0-3=3, 3-5=2, n-5=2 => max_gap=3, l=3 works. Greedy: 0->3, cur=6, largest <=6 is 5? Actually 5 <=6, yes. So lamp at 5, cur=8>=7. Works.
Now consider a case where there is a blocked at 0? Already covered; answer -1.
Now consider case where 0 allowed, but there is a gap greater than l, then impossible. So condition seems necessary and sufficient.
But we must also consider that we might be able to cover the segment even if there is a gap > l, by using multiple lamps? Let's think: if there is a gap between two allowed positions p_i and p_{i+1} where p_{i+1} - p_i > l, can we still cover the street? Suppose p_i and p_{i+1} are consecutive allowed positions. Then there is a blocked region between them. To cover the point at p_i+? Actually we need to cover the entire interval [0,n]. In particular, the point just after p_i up to before p_{i+1} must be covered. Lamps can only be placed at allowed positions. The leftmost lamp after p_i must be placed at some allowed position <= p_i (maybe p_i itself) to cover the region near p_i. The lamp at p_i covers up to p_i + l. To cover the point p_i + l + epsilon, we need another lamp whose coverage starts at some x <= p_i + l (to cover continuously) and covers beyond. The next allowed position is p_{i+1}. But if p_{i+1} > p_i + l, then there is a gap: from p_i + l to p_{i+1} is uncovered because any lamp placed at positions <= p_i + l (and allowed) cannot be at p_{i+1} because p_{i+1} is > p_i + l. The only possible lamp that could cover the region after p_i + l is a lamp placed at some allowed position x such that x <= p_i + l (to cover continuously) and x + l > p_i + l. But if x <= p_i + l, then x + l <= p_i + 2l. However, the uncovered gap is from p_i + l to p_{i+1}. If p_{i+1} > p_i + l, we need to cover that gap. The next lamp must be placed at an allowed position x that is > p_i? But if the next allowed is p_{i+1} > p_i + l, then x cannot be <= p_i + l because x would have to be p_{i+1} (since it's the next allowed) but p_{i+1} > p_i + l, so x > p_i + l. However, if x > p_i + l, then the interval [x, x+l] starts after p_i + l, leaving a gap between p_i + l and x. So we cannot cover that gap. Thus impossible. So indeed gap > l implies impossibility. So condition is necessary.
Also need that the last allowed covers n: if p_last + l < n, then from p_last + l to n is uncovered, impossible. So necessary.
Thus feasibility condition for power l is: l >= max_gap, where max_gap = maximum over:
- gaps between consecutive allowed positions (including from 0 to first allowed after 0? 0 is allowed so gap from 0 to next allowed)
- and the gap from the last allowed to n.
But wait, what about the gap before the first allowed? Since 0 must be allowed, there's no gap before 0.
Thus, if we precompute the maximum gap G (including tail gap) among the allowed set (with 0 allowed), then any l >= G is feasible? Actually if l >= G, then condition holds, so feasible. If l < G, then not feasible. So feasibility is simply l >= G. But is that always true? Let's test with examples.
Example 1: n=6, m=2, blocked:1,3. So allowed: 0,2,4,5? Wait positions from 0 to n-1=5? Actually n=6, positions 0..5. Blocked 1 and 3. So allowed: 0,2,4,5? Check: 0 allowed, 1 blocked, 2 allowed, 3 blocked, 4 allowed, 5 allowed? Yes. Sorted: 0,2,4,5. Gaps: 2-0=2, 4-2=2, 5-4=1. Tail gap: n - last_allowed = 6 - 5 = 1. So max_gap = 2. So feasible if l >=2. k=3, costs: a1=1, a2=2, a3=3. So l=2 and 3 are feasible. For l=2, compute minimal number of lamps? We need to compute number of lamps to get total cost. For l=2, greedy: 0->covers 2, cur=2, largest allowed <=2 is 2, lamp at 2, cur=4, largest allowed <=4 is 4, lamp at 4, cur=6 => done. Lamps: 0,2,4 = 3 lamps. cost = 32=6. For l=3: 0->cur=3, largest allowed <=3? Allowed: 0,2,4,5. Largest <=3 is 2. So lamp at 2, cur=5, largest allowed <=5 is 5? Actually 5 <=5, so lamp at 5, cur=8. Lamps: 0,2,5 = 3 lamps? Actually 0,2,5: 3 lamps. cost=33=9. So minimal cost is 6. Output 6 matches example.
But what about l=1? l=1 < max_gap=2, so infeasible? Let's check manually: allowed:0,2,4,5. With l=1, lamps placed only at allowed positions. Need to cover [0,6]. Start with lamp at 0 covers [0,1]. Then we need to cover from 1 to 6. Next lamp can be placed at allowed <=1: only 0? Because 2>1. So we can't progress. So indeed infeasible. So condition holds.
Example 2: n=4, m=3, blocked:1,2,3. So allowed: only 0? Positions 0..3. Blocked 1,2,3, so allowed:0. Gaps: tail gap = n - 0 =4. max_gap=4. So l must be >=4. k=4, costs: a1=1,a2=10,a3=100,a4=1000. So l=4 is feasible, cost = 1*1000? Wait lamp cost a4=1000, need one lamp? With l=4, lamp at 0 covers [0,4] (since n=4). So one lamp cost 1000. That's output. For l<4, infeasible. So matches.
Example 3: n=5, m=1, blocked:0. So allowed: positions 1,2,3,4? Since 0 blocked, condition fails immediately. So -1. Yes.
Example 4: n=7, m=4, blocked:2,4,5,6. So allowed: positions 0,1,3? Check: positions 0..6. Blocked:2,4,5,6. So allowed: 0,1,3? Also 7? n=7, positions up to 6. So allowed: 0,1,3. Sorted: 0,1,3. Gaps: 1-0=1, 3-1=2. Tail gap: n - 3 = 7-3=4. So max_gap = max(1,2,4)=4. But k=3 (max power 3). So max allowed l is 3 < 4, so infeasible for all l. Output -1. Matches.
Thus, the feasibility condition seems correct: for a given l, it is possible iff l >= G, where G = max( max_gap_between_consecutive_allowed, n - last_allowed ). And also 0 is allowed (if not, answer -1 regardless). Wait, if 0 is blocked, then it's impossible for any l, because we cannot cover point 0. So overall answer is -1 if 0 blocked. Otherwise, compute G. Then any l in [G, k] is feasible. And we need to compute minimal cost = min_{l=G..k} ( (min number of lamps needed for l) * a_l ).
Now we have to compute for each feasible l the minimal number of lamps (i.e., the number of lamps used by greedy algorithm). Since we now have that feasibility only depends on G, but the number of lamps for each l may vary. We need to compute the minimal number of lamps needed for each l, and then compute cost = num_lamps(l) * a_l, and take the minimum.
Now the problem reduces to: given the allowed positions (including 0), we need to compute for each l from 1 to k (or at least from G to k) the number of lamps required in the optimal covering using power l, assuming it's feasible (i.e., l >= G). But note: if l < G, it's infeasible, we can ignore.
So we need an efficient way to compute num_lamps(l) for all l in [G, k]. n up to 1e6, k up to n.
We need to compute the greedy step count: start at p0=0, cur = 0 (but after first lamp we set cur = l? Actually we can consider the process: count = 1 initially (lamp at 0). cur = l. While cur < n: let x = largest allowed <= cur; then cur = x + l; count++. This yields number of lamps.
But we can also think of it as the number of steps in the sequence: positions visited: i0 = index of p0 (0). Then while true: find the next index j such that p_j <= p_i + l, and take the largest such j (i.e., the furthest allowed within reach). Then set i = j, increment count. Continue until p_i + l >= n.
This is similar to a jump game on the sorted allowed positions array, where from index i you can jump to the largest index j such that p_j - p_i <= l? Actually condition is p_j <= p_i + l. So p_j - p_i <= l. So essentially we can jump from p_i to any p_j with distance at most l, and we want to maximize the jump (i.e., go to the farthest p_j within distance l). That's exactly a greedy for covering with intervals of length l starting at allowed points: you always jump to the farthest reachable allowed point. The number of jumps (including the starting point) is the number of lamps.
Thus, num_lamps(l) = minimum number of jumps from index 0 to reach a point where p_i + l >= n, using jumps to the farthest index within distance l.
Now we need to compute this for many l. This is reminiscent of a "jump distances" problem that can be solved by precomputing next pointers for each index and then using binary lifting for each l? But l varies, the reach condition changes.
We could precompute for each allowed position i, for each possible l, the next jump index. That's too much.
But note that the allowed positions are a subset of [0, n-1] of size up to n. Since n is up to 1e6, we could perhaps compute for each l the number of lamps by simulating using a pointer that moves along the allowed list, but the number of steps for each l may be O(n/l), and sum over l from 1 to n of n/l is n * H_n ~ n log n, which is about 1e6 * ~14 = 14e6, manageable! Wait, careful: n=1e6, sum_{l=1}^{n} (n/l) ~ n * log n = about 20 million. That's feasible. However, we only need to simulate for l >= G (maybe down to 1 if G small). But worst-case G could be 1, so we need to simulate for all l from 1 to k (which could be up to n). So sum_{l=1}^{n} steps = O(n log n) seems okay. But each step involves finding the next allowed position? In greedy, each step we need to find the largest allowed <= cur. If we simulate naively for each l, we might need to scan through allowed positions each time, which could be O(n) per l, not O(n/l). But if we use a pointer that advances through the list of allowed positions, we can compute number of steps in O(#steps + something). Actually we can simulate the greedy for a given l in O(#steps + 1) if we can quickly find, for each current allowed index, the next index j (the furthest within l). That requires binary search or two-pointer technique. But we can implement a two-pointer approach: Pre-sort allowed positions array