Petya has equal wooden bars of length n. He wants to make a frame for two equal doors. Each frame has two vertical (left and right) sides of length a and one top side of length b. A solid (i.e. continuous without breaks) piece of bar is needed for each side.
Determine a minimal number of wooden bars which are needed to make the frames for two doors. Petya can cut the wooden bars into any parts, but each side of each door should be a solid piece of a wooden bar (or a whole wooden bar).
Input
The first line contains a single integer n (1 ≤ n ≤ 1 000) — the length of each wooden bar.
The second line contains a single integer a (1 ≤ a ≤ n) — the length of the vertical (left and right) sides of a door frame.
The third line contains a single integer b (1 ≤ b ≤ n) — the length of the upper side of a door frame.
Output
Print the minimal number of wooden bars with length n which are needed to make the frames for two doors.
Examples
Input
8
1
2
Output
1
Input
5
3
4
Output
6
Input
6
4
2
Output
4
Input
20
5
6
Output
2
Note
In the first example one wooden bar is enough, since the total length of all six sides of the frames for two doors is 8.
In the second example 6 wooden bars is enough, because for each side of the frames the new wooden bar is needed.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to interpret the problem carefully. There are two identical doors, each requiring a frame. Each frame consists of two vertical sides (left and right) each of length a, and one top side of length b. So total for one door: 2a + b. For two doors: total length required = 2*(2a + b) = 4a + 2b.
We have unlimited wooden bars each of length n. We can cut them into pieces, but each side must be a solid piece (i.e., we cannot glue or join pieces to make a side). So each side must be cut from a single bar (maybe also whole bar). We need to find the minimal number of bars needed to obtain these pieces: 2 vertical sides of length a per door? Actually careful: Two doors: total sides needed: vertical sides: each door has two vertical sides, so 2 doors * 2 = 4 pieces of length a. And horizontal top sides: each door has one top side of length b, so total 2 pieces of length b.
Thus we need to produce 4 pieces of length a and 2 pieces of length b. Pieces are solid, each must be from some bar, possibly with leftover waste.
We can cut each bar of length n into several pieces, but the total sum of lengths of pieces taken from a bar cannot exceed n. The question: minimal number of bars required to produce these required pieces.
This is a classic cutting stock problem with small numbers (n ≤ 1000, but the numbers of pieces are small: 4 and 2). We can try to brute force possible assignments of pieces to bars. Since the numbers are small, we can enumerate ways to pack pieces into bars, perhaps using integer linear programming or search. But n, a, b up to 1000, but the number of bars needed may be up to? In worst case maybe 6 (if each piece needs a separate bar because a and b > n/2 maybe). So we can search all possible partitions.
We need to consider that pieces are of two types: a and b. We have 4 a's and 2 b's.
Goal: minimize number of bars used such that each bar's total length ≤ n.
Because bars are identical, we need to find a multiset of bins (bars) of capacity n, containing these items: 4 items of size a, 2 items of size b. Items are indistinguishable except by size.
We need minimal number of bins. This is bin packing with small items, but we can solve by considering all possible patterns of how many a and b can fit in a bar, then solve a covering/partition problem: we need to select patterns (bars) such that total counts of a and b meet or exceed requirements? Actually we need exactly the pieces, so we need a combination of patterns that sum to exactly 4 a and 2 b. The patterns are feasible sets of pieces (a's and b's) that fit in a bar: i.e., integers x (number of a pieces) and y (number of b pieces) such that xa + yb ≤ n, with x,y ≥0 integers.
Since we are minimizing number of bars, we need to cover the required items with as few patterns as possible. This is a small integer programming: minimize sum of z_k (bars) subject to for each pattern p, choose count z_p, and sum_p z_p * x_p >= 4? Actually we need exactly 4 a, but if we use patterns that may have more a than needed? We can't have extra pieces; we must produce exactly those pieces. But if a pattern contains more items than needed, we could cut them and waste the rest? No, we can't have extra pieces because we need exactly 4 a and 2 b. If a pattern yields more a pieces than needed, we would have to produce them, but then we would have extra sides, which we don't need. But we could produce them and discard? The problem says: "Petya wants to make a frame for two equal doors." So he needs to produce exactly those pieces. He can cut bars into any parts, but he doesn't have to use all cut pieces. So he can cut a bar into, say, three pieces: two a's and one b, but if he only needs certain pieces, he could discard some pieces. However, does the problem allow discarding? Typically, in such cutting problems, we need to obtain the required pieces; we can cut the bars and may have waste, but we cannot produce extra required pieces? Usually, you are allowed to cut and keep the needed pieces; you are not forced to use all cut pieces. So if a pattern contains more a's than needed, you could produce those but then you'd have extra a pieces, which you don't need. That would be unnecessary but wouldn't prevent making the frames; you would just have extra material. However, does the problem require that all cut pieces are used? It says: "Determine a minimal number of wooden bars which are needed to make the frames for two doors." So we need to be able to obtain the necessary pieces from the bars. If we cut a bar and get extra pieces, that's allowed as long as we can get the required pieces. But using extra bars increases the count. However, could using a pattern that produces more than needed reduce number of bars? Possibly yes, because you might pack more items into one bar, but if you overproduce one type, you might still cover requirements with fewer bars, but you'd have extra pieces. Is that allowed? Typically in such problems, you are allowed to cut bars and then discard leftover pieces; you don't have to use all pieces. But you are not allowed to produce extra required pieces? Usually, the goal is to obtain the required pieces, so you can have leftover waste, but you cannot have extra pieces of the required dimensions? I think it's fine to have extra pieces, because you can just not use them. But does the problem imply that you must use the entire bar? The problem statement: "Petya can cut the wooden bars into any parts, but each side of each door should be a solid piece of a wooden bar (or a whole wooden bar)." It doesn't say you have to use all parts. So it's okay to have waste and also extra pieces that are not used. So we can cut more pieces than needed. However, minimizing the number of bars, we want to use as few bars as possible. If we produce extra pieces, that doesn't violate anything. So the constraint is: we need to be able to obtain at least 4 pieces of length a and at least 2 pieces of length b from the cut bars. Because we can discard extras. So we need to cover the demands (4,2) with possibly extra pieces. But wait: Could we get into a situation where we produce extra pieces that still allow us to meet demand with fewer bars? For example, suppose we have a bar that can fit 3 a's. Then using one such bar gives 3 a's, but we need 4 a's, so we still need another bar to get at least 1 more a. That second bar might produce some b's as well. So total bars maybe 2. If we instead tried to exactly produce 4 a's and 2 b's without extras, maybe we would need 3 bars. But if we allow extras, we might use fewer bars. But is it possible to use fewer bars by producing extra pieces? That would mean that by producing extra pieces, we can combine items in bars more efficiently. However, we are already allowed to pack items arbitrarily; if we produce extra pieces, we are essentially "wasting" capacity on extra pieces, which could reduce the efficiency of packing required pieces, not improve it. Because if we pack extra pieces, we are using capacity that could have been used for other required pieces. So to minimize bars, we would never intentionally produce extra required pieces, because that would take up capacity that could be used to cover other required pieces, potentially increasing the number of bars needed. So the optimal solution will not have any extra required pieces; it will have exactly the required counts, or possibly extra pieces if unavoidable due to integrality? But we can always cut bars to produce exactly certain pieces; if a pattern yields extra pieces, we could just not cut those pieces. Actually, we have control over how we cut. So we can cut a bar to produce exactly a set of pieces that we want, as long as the sum of their lengths ≤ n. So we can choose any combination (x a, y b) with xa+yb ≤ n. We don't have to cut the bar into all possible pieces; we can cut it into exactly those pieces we intend to use. So we can avoid producing extra pieces. So the problem reduces to: partition the multiset of required pieces (4a, 2b) into subsets (each subset assigned to a bar) such that the total length of each subset ≤ n. Find minimal number of subsets (bars). This is exactly bin packing where we need to pack exactly these items.
Thus, we need to pack 6 items (4 of size a, 2 of size b) into bins of capacity n, minimize bins.
Since item count is very small (6), we can brute force all possible assignments. The complexity is manageable: for each bar, we choose a combination of remaining items that fit. Use backtracking.
Alternatively, because numbers are small, we can manually reason about possible patterns.
We can treat it as: We need to find the minimal m such that we can partition the 6 items into m bins each with sum ≤ n.
Given n, a, b, with a,b ≤ n. So each item fits individually.
Possible m ranges from 1 to 6. Check if 1 bar works: total sum = 4a+2b ≤ n? If yes, then answer 1. If not, check m=2,3,4,5,6.
We can test m=2: need to see if we can split the 6 items into two groups each sum ≤ n. Equivalent to see if we can pack them into two bins. Since bins are identical, we can think: one bin gets some items, the other gets the rest, and both must be ≤ n. Since total sum S = 4a+2b, if we can partition such that both bins ≤ n, then possible. For 2 bins, we need each ≤ n, and also the sum of both is S, so each must be at least S - n (since the other can be at most n). So we need max(item sizes) ≤ n and also S ≤ 2n (which is automatically true if each a,b ≤ n? Not necessarily: 4a+2b could exceed 2n if a and b are large. But if S > 2n, then impossible with 2 bins, because total capacity 2n would be insufficient. So necessary condition: S ≤ 2n. But also we need to be able to allocate items such that each bin ≤ n. This is a bin packing feasibility.
Since only 6 items, we can try all possible splits among two bins. But we can also think in terms of the largest possible sum in one bin. We want to see if we can assign items so that no bin exceeds n.
Given that we have 4 a and 2 b, the "hardest" part is to avoid a bin exceeding n. Usually, we can try to balance.
Alternatively, we can compute the minimal possible maximum bin load for a given number of bins? But given small numbers, brute force is fine.
For m=3, we check if we can partition into 3 bins each ≤ n.
Since we need to output minimal number, we can iterate m from 1 upward and check feasibility.
Given that there are only 6 items, the maximum m needed is 6 (worst-case each item alone, but if a and b each ≤ n, each item fits in one bar, so max 6). So we can just try m=1..6.
We can implement a recursive bin packing feasibility check: assign items to bins (with capacity n) one by one, using pruning.
Because number of items is small, we can also precompute all possible subsets that fit in a bin, and then try covering the multiset with m subsets.
But we can also manually enumerate possibilities because there are only two types of items. This can be solved by analyzing possible patterns for each bar, then solving a small integer equation.
Let's formalize:
We need nonnegative integers x_i, y_i for each bar i=1..m, such that x_i * a + y_i * b ≤ n, and sum_i x_i = 4, sum_i y_i = 2. Also x_i, y_i integers. m is number of bars. We want minimal m.
So we need to find if there exist m such that we can choose x_i, y_i with constraints.
We can treat the total required as 4a+2b. For a given m, we need to allocate the items into m bins, each bin with capacity n.
We can approach by checking m=1: check if 4a+2b ≤ n.
If not, m=2: we need to see if there is a solution to:
Find nonnegative integers x1,y1,x2,y2 such that:
x1+x2 =4, y1+y2=2,
ax1 + by1 ≤ n,
ax2 + by2 ≤ n.
Equivalently, we need to partition the 4 a's and 2 b's into two groups, each group sum ≤ n.
We can attempt to enumerate possible x1 from 0 to 4, y1 from 0 to 2, and check if sum1 ≤ n and remaining sum2 = (4-x1)*a + (2-y1)b ≤ n. That's at most 53=15 checks. So easy.
For m=3: enumerate x1,y1 for first bin, then recursively? Or we can brute force all possible distributions among three bins: we need to allocate 4 a and 2 b to three bins such that each bin sum ≤ n. Since bins are unlabeled, we need to check if any allocation exists. We can enumerate all possible triples (x_i,y_i) for i=1..3 that sum to totals. But number of possibilities is not huge: each bin can have x from 0 to4, y from0 to2, but we have totals. Could use recursion/backtracking.
But because m is at most 6, we can write a simple backtracking algorithm: try to place items one by one into bins (with capacity) and see if we can fit all items into m bins. Since items are not distinct but have sizes, we can treat them as multiset. Use DFS with bin assignment and pruning by sorting bins.
Alternatively, we can reason based on the values. But to be safe, we can implement a generic feasibility check for given m using integer linear programming or recursion with memoization. But given the small size, we can just precompute all possible valid patterns (x,y) that fit in a bar, then we need to choose m patterns such that sum of x =4, sum of y=2. This is a small integer combination problem: for m=1, check if pattern (4,2) is valid (i.e., 4a+2b ≤ n). For m=2, check if there exist two patterns (x1,y1), (x2,y2) valid such that x1+x2=4, y1+y2=2. For m=3, check if there exist three patterns with sums 4 and 2. And similarly for m=4,5,6. We can generate all valid patterns (x,y) (x from 0 to floor(n/a), y from 0 to floor((n - x*a)/b)). But note that we don't need patterns with large x,y beyond totals. So we can limit x ≤4, y ≤2. Then we have a list of patterns.
Then for a given m, we need to determine if we can select m patterns (allowing repetition) such that sum of x =4 and sum of y=2. This is a bounded integer composition problem. Since m is small, we can use recursion: pick patterns for each bar, sum up.
But we need to be careful: patterns can be repeated. Since m ≤6 and pattern count is small, we can use recursion with pruning.
Simplify: because we have only 6 items, the best is to think in terms of packing items into bins, which might be easier: we have a list of item sizes: four of size a, two of size b. We want to pack them into m bins of capacity n. This is a bin packing decision problem. We can use brute force: for each m, generate all assignments of items to bins (with bins considered identical, we need to avoid symmetries, but we can just try to fill bins sequentially with items, using backtracking and pruning by bin capacity, and check if we can assign all items. This is like: try to place each item (6 items) into one of m bins, each bin has current total ≤ n. Use DFS. Since m ≤6, branching factor ≤ m at each step, total possibilities: m^6, but with pruning it's okay because 6^6=46656, that's fine. For each test case, we check m from 1 to 6, total up to ~ 6*46656=~280k, which is fine. n up to 1000, but we only need to compare sizes.
We can further optimize: we only have two distinct sizes, so we can treat them as counts. But the simple DFS assignment of each distinct item (considering items distinguishable? Actually if items of same size are indistinguishable, we can treat them as counts to reduce state). But even the simple DFS with 6 items (like a list of sizes: [a,a,a,a,b,b]) and assign each to one of m bins, and check capacities, would be fine.
But we must consider that bins are identical; we can reduce symmetries by ordering bins, e.g., when placing an item, we only consider bins that currently have the same total as a previous bin to avoid duplicate assignments. However, given small sizes, we can just allow any assignment, and it will still be fine.
We'll implement a function canPack(m) that attempts to assign items to bins via recursion. We'll sort the items descending to improve pruning. For each item, we try to place it into each bin that has enough remaining capacity, skipping bins that are equivalent in state to avoid duplicates? Actually, we can implement as: we have an array of m bins, initially 0. We have a list of items (sizes). We try to place items one by one. For each item, we try each bin that can accommodate it. To avoid exploring many symmetric states, we can enforce that we only place an item into a bin that is currently empty or has the same total as previous bins? Or we can simply try all bins and rely on small m. 6^6 = 46656, that's fine. But 6 items with m up to 6: worst-case m=6, we have 6 items, each can go to any of 6 bins, that's 6^6 = 46656. That's for one m. For m from 1 to 6, total less than 200k. That's negligible.
We also need to handle cases where m is less than 6, but then branching factor is smaller. So overall fine.
Thus we can implement:
for m in 1..6:
if canPack(m): output m and break.
Implementation of canPack(m):
bins = [0]*m
items = [a]*4 + [b]*2 (list of sizes)
sort items descending (largest first) for better pruning.
define recursive function dfs(index):
if index == len(items): return True
for i in range(m):
if bins[i] + items[index] <= n:
bins[i] += items[index]
if dfs(index+1): return True
bins[i] -= items[index]
# Additional pruning: if bins[i] == 0 then break? Because if we place item in an empty bin and there are other empty bins, it's symmetric. So we can avoid placing item in a bin that is empty if we already tried an empty bin earlier. So we can do: if bins[i] == 0: break after trying one empty bin? Actually common optimization: when placing an item, we consider each bin, but if we encounter a bin that is currently empty, we only need to try one of them because all empty bins are equivalent. So we can add: after we try placing in an empty bin, we skip subsequent empty bins. Implement: if bins[i] == 0: then after trying, set a flag to not try other zeros. But careful: we might need to consider that we could place in different empty bins later? However, because items are indistinguishable? Actually if we have multiple empty bins, placing an item in any empty bin yields the same state (since all empty bins are identical). So we can just try one. So we can modify: for i in range(m): if bins[i] + items[index] <= n: then if bins[i]==0 and we have already used an empty bin? We can track a variable "used_zero" to avoid repeating. More simply, after trying a bin that is empty, we can break after recursion because trying other empty bins would lead to symmetric states. But careful: we need to ensure we still explore possibilities where we put the item in a non-empty bin as well. So we can do: first try non-empty bins (maybe any order). Actually typical approach: iterate over bins, but when we encounter a bin with capacity = 0 (i.e., currently empty), we try it, and then after that we set a flag to not try any other empty bins (i.e., break after the first empty bin). This works if we ensure that we sort bins in some order. For simplicity, we can skip optimization; 46656 is fine.
But we might have up to multiple test cases? The problem appears to be a single test case (one input). So we just read n, a, b and compute answer.
Thus we can implement DFS. However, note that n, a, b are up to 1000, but we only compare sums, so no overflow.
Edge cases: a and b could be zero? No, they are ≥1. So fine.
Example 2: n=5, a=3, b=4. S=43+24=12+8=20. 20 > 5, so m>1. Check m=2: total capacity 2*5=10 < 20, impossible. m=3: capacity 15 <20, impossible. m=4: capacity 20 =20. But we need to see if we can pack items into 4 bins each capacity 5. Items sizes: 3,3,3,3,4,4. Each bin capacity 5. Can we pack? Let's check: 4's need bins: each 4 can be in a bin with maybe a 1? But we have only 3's and 4's. 4+3=7 >5, so each bin can contain at most one item, either a 3 or a 4. So we need at least 6 bins because we have 6 items. But wait, a 3 fits, a 4 fits, but we cannot put two items together because any two sum >5 (3+3=6, 3+4=7, 4+4=8). So m must be at least 6. So answer 6. Indeed, output 6.
Example 3: n=6, a=4, b=2. S=44+22=16+4=20. Capacity per bar 6. m=1: 20>6 no. m=2: total cap 12<20 no. m=3: cap 18<20 no. m=4: cap 24≥20. Check if we can pack 4 items of size 4 and 2 items of size 2 into 4 bins of capacity 6. Items: 4,4,4,4,2,2. We need to pack them. Let's try: Since 4+2=6 exactly, we can pair each 2 with a 4. We have 4 fours and 2 twos. So we can have two bins with (4+2)=6, and two bins with just a 4 (since 4≤6). That's 4 bins total. So answer 4. Yes.
Example 4: n=20, a=5, b=6. S=45+26=20+12=32. Capacity per bar 20. m=1: 32>20 no. m=2: total capacity 40≥32. Check if can pack into 2 bins. Items: four 5's (each 5) and two 6's (each 6). Total 32. Need to split into two groups each ≤20. Can we? Try to see if one bin can have sum ≤20, the other automatically total 32 - that sum, must also ≤20 => the sum must be at least 12 (since 32-20=12) and ≤20. So we need a subset of items with total between 12 and 20 inclusive. Is there? Let's see possible combinations: 5+5+6=16, which fits. Then remaining would be 5+5+6=16 also? Actually we have 4 fives and 2 sixes. If one bin takes 5+5+6=16, the other gets 5+5+6=16, both ≤20. So yes, two bins suffice. So answer 2.
All matches.
Thus a brute force bin packing decision per m works.
But we can also directly compute answer using some reasoning. However, brute force is simple and robust.
def dfs(idx):
if idx == len(sizes):
return True
for i in range(m):
if bins[i] + sizes[idx] <= n:
bins[i] += sizes[idx]
if dfs(idx+1):
return True
bins[i] -= sizes[idx]
# If bins[i] was 0 before adding, then break because trying other empty bins is symmetric
# optional optimization: if bins[i] == 0: break
return False
return dfs(0)
But we need to be careful: the DFS may explore many symmetric states due to bins being identical. The optimization: after trying to place an item into an empty bin, we skip remaining empty bins because they are equivalent. Implementation: in the loop, we can track if we have already placed the item into a bin that was empty before (i.e., bins[i] == 0). If we have, then after recursing and backtracking, we break to avoid trying other empty bins. This greatly reduces branches. Also, we can sort bins? Actually we can just do:
for i in range(m):
if bins[i] + sizes[idx] <= n:
bins[i] += sizes[idx]
res = dfs(idx+1)
bins[i] -= sizes[idx]
if res:
return True
if bins[i] == 0:
break
return False
Why break after bins[i]==0? Because if we attempted placing the item in a bin that was empty and it failed, placing it in another empty bin would be the same (since all empty bins are identical). So we can prune.
This is a common optimization for bin packing DFS.
Also, we may want to sort bins by capacity remaining? Not needed.
Given the small m, even without optimization it's fine.
Edge Cases: If a or b > n? Problem says a,b ≤ n, so each item individually fits in a bar.
Thus answer at most 6.
We need to output minimal number.
Now implement solve():
Read n, a, b.
For m in range(1,7):
if can_pack(m):
print(m)
return
But ensure we only go up to 6; if somehow no solution (impossible) but there is always solution with 6 because each item fits individually, so m=6 always works. So loop will terminate at 6.
Testing:
Example 2: n=5, a=3, b=4. For m=4, capacities sum 20 but need to pack. Let's see if DFS finds solution? Actually, with m=4 we can try to pack 6 items into 4 bins. Since each bin capacity 5, each bin can hold at most one item (3 or 4). So we need at least 6 bins, so m=4 fails. m=5: capacity sum 25, but each bin can hold only one item, with 5 bins we have 5 slots, but we have 6 items, so need 6 bins. So m=6 works. So answer 6.
Example 4: n=20, a=5, b=6. m=2: should succeed.
Thus implementation is straightforward.
But I recall that there might be a nuance: The frame also might have bottom? The problem statement: "Each frame has two vertical (left and right) sides of length a and one top side of length b." It does not mention a bottom side. So it's like a U-shaped frame? Possibly it's only three sides? Yes, just three sides per door. That's what we used.
Thus solution is bin packing.
Now, we could also try to compute analytically: The minimal number is ceil((4a+2b)/n) if we could cut arbitrarily and combine? But not always, because bin packing can require more bins than the lower bound of total sum / n due to indivisibility. For example, n=5, a=3,b=4 gives total sum 20, ceil(20/5)=4, but actual needed is 6. So lower bound is ceil(S/n). However, the answer may be larger due to capacity constraints. So we can compute lower bound L = ceil(S/n). Then we need to check if packing is possible with L bins, else try L+1, etc., up to 6. Since L ≤ 6 (because S ≤ 4n+2n=6n? Actually a,b ≤ n so max S = 4n+2n = 6n, so ceil(S/n) ≤ 6). So we can start from L.
But to be safe, we can just iterate from 1 to 6.
Nevertheless, using DFS with m from L upward would be slightly more efficient, but not necessary.
Now, note that in some cases, even if total sum fits within m*n, packing might be impossible due to large items. For example, if a > n/2? Actually each item ≤ n, so any single item fits. But if we have two items that sum > n, they cannot be in same bin. So we need to consider that.
Simplify: Since we have only 4 a and 2 b, we can precompute all possible ways to pack into m bins by checking feasible allocations of counts.
Better to implement DFS with counts: because items of same size are indistinguishable, we can treat bins as capacities and use recursion over types rather than individual items. This might be more efficient and simpler to implement.
We can define a recursive function that assigns items to bins using counts. But the DFS over bins with capacity could be trickier. Instead, we can use a pattern-based approach.
Let's consider: For given m, we need to decide how many a's and b's go to each bin. Since bins are identical, we can consider the multiset of patterns (xi, yi). So we need to find a multiset of m patterns from the set of all feasible patterns P = {(x,y): xa + yb ≤ n, 0 ≤ x ≤ 4, 0 ≤ y ≤ 2} such that sum x =4, sum y=2.
Then we can try to recursively select patterns with counts.
But the DFS over patterns with limited m (≤6) and small pattern list is also simple.
Alternatively, we can use integer programming via nested loops for m=2,3,4,5,6. Since m small, we can manually handle each case with loops. For m=2, we already have double loop. For m=3, we can have triple loop over patterns? But patterns are many? But x from 0 to floor(n/a) up to maybe 1000, but we can limit to at most 4 and y at most 2, so at most (5*3)=15 patterns. So for m=3, we can have three nested loops over pattern indices (allowing repetition) and check sums. But three nested loops over 15 patterns = 3375 combos, times for m=3, plus we need to also consider ordering? Since bins are unlabeled, we may overcount but it's fine; we can just break when found. That's acceptable. For m=4, 15^4=50625, still okay. For m=5, 15^5=759375, maybe borderline but still okay given single test case. For m=6, 15^6~11 million, might be slower but still possibly okay in Python if optimized? But we can prune by early exit and also we can use recursive search with backtracking summing to targets. However, DFS with items (6 items) is simpler and more direct.
Thus I'll go with the item-based DFS.
Implementation of DFS with pruning:
def can_pack(m):
items = [a]*4 + [b]*2
# optional: sort descending
items.sort(reverse=True)
bins = [0]*m
# To reduce symmetry, sort bins? We'll use the "if bins[i]==0: break" trick.
def dfs(idx):
if idx == len(items):
return True
for i in range(m):
# To further reduce, we can skip bins that have same capacity as previous to avoid duplicates? But not needed.
if bins[i] + items[idx] <= n:
bins[i] += items[idx]
if dfs(idx+1):
return True
bins[i] -= items[idx]
# If after backtracking the bin was empty before addition, then we break because all other empty bins are same.
if bins[i] == 0:
break
return False
return dfs(0)
But careful: The condition "if bins[i] == 0" after backtracking? Actually we need to know if the bin was empty before we added the item. At this point, after we subtract, bins[i] is back to its original value. So we can check if bins[i] == 0 before adding? In the code, after backtracking, bins[i] is the original value. So if original value was 0, then bins[i] is 0 now. So the condition is correct: if bins[i] == 0, then we break, because trying other bins with also 0 would lead to symmetric states. However, we must ensure that we also try non-zero bins first? The loop is in order from 0 to m-1. If we encounter a zero bin and place the item there, after backtracking, we break, so we never try other zero bins. But what if we didn't try placing in a non-zero bin before that zero bin? The order matters. Ideally, we should try to place the item in a non-zero bin first, because placing in an empty bin may lead to more bins used, but our DFS will explore both possibilities. To ensure we don't miss solutions due to pruning, we need to be careful: the break after a zero bin is only safe if we have already tried all possibilities where the item goes into a non-zero bin? Actually the logic is: When we consider placing the current item, we iterate over bins. For each bin, if it fits, we try. After we try a bin and it fails, if that bin was empty (i.e., before adding, bins[i]==0), then any other empty bin would be identical, so if it failed in this empty bin, it would also fail in any other empty bin, because the state after placing the item would be identical: the chosen bin would have the item, all other bins unchanged. So we can skip other empty bins. However, we must ensure that we only apply this pruning after we have tried the placement in that empty bin and it returned False. It's safe because we have already explored the subtree where this specific empty bin holds the item. Since all empty bins are symmetric, exploring any other empty bin would produce the same set of states (just with labels swapped), so no need. So it's safe to break after trying the first empty bin that fits, regardless of order. But what if we first encounter a non-zero bin, try it, it fails, then later encounter an empty bin, try it, it fails, then we break. That's fine. But what if we encounter an empty bin first, try it, it fails, then break, we never try non-zero bins? That would be a problem because maybe placing the item in a non-zero bin could lead to a solution. So we must ensure that we try non-zero bins before we get to the empty bin? The break condition is after trying a bin that is empty and fails; if we haven't tried any non-zero bins yet, we would break prematurely. So we need to order the loop so that we consider non-zero bins first, then empty bins. Or we can modify the pruning to only break if bins[i]==0 after backtracking, but we also need to ensure we have considered all non-zero bins before breaking. But the loop order is fixed; if we iterate i from 0 to m-1, we might hit an empty bin early. To avoid missing solutions, we can sort bins in order of decreasing current load before the loop, or we can first try bins that already have some load, then empty ones. However, a simpler approach: we can use the condition "if bins[i] == 0: break" only after we have already tried all bins? Actually, a common idiom is:
for i in range(m):
if bins[i] + item <= capacity:
if bins[i] == 0 and zero_bin_used:
continue # skip because we already tried an empty bin
bins[i] += item
if dfs(idx+1): return True
bins[i] -= item
if bins[i] == 0:
zero_bin_used = True
But we need to track whether we have already used an empty bin. Alternatively, we can implement as:
Keep a variable "last" to skip subsequent zero bins after the first zero bin is attempted.
Simplify: we can just not use the optimization, since 6^6 is small.
Given that we have at most 46656 states for m=6, and each state does a loop of up to m, total operations maybe up to ~ 280k, which is trivial. So we can skip optimization and just use simple DFS with bins array and no symmetry breaking. That will be fine and less error-prone.
Thus:
def can_pack(m):
items = [a]*4 + [b]*2
# maybe sort descending to help prune earlier? Not necessary but good.
items.sort(reverse=True)
bins = [0]*m
code
def dfs(idx):
if idx == len(items):
return True
for i in range(m):
if bins[i] + items[idx] <= n:
bins[i] += items[idx]
if dfs(idx+1):
return True
bins[i] -= items[idx]
return False
return dfs(0)
But note: Without symmetry breaking, the number of states may be larger than m^6 because we prune by capacity and items may be same size. However worst-case when all items are small and can fit in any bin, the DFS will explore many assignments. For example, n=1000, a=1, b=1, then all items size 1. We need to pack 6 items into m bins. The number of ways to assign 6 indistinguishable items to m bins is C(6+m-1, m-1) which for m=6 is C(11,5)=462. Actually if items are identical (all size 1), the DFS will treat items as distinct? In our implementation, items are stored as a list of sizes; we treat each item as distinct (by position), even if they have same size. That leads to many permutations: for m=6, the number of assignments of 6 distinct items to 6 bins is 6^6 = 46656. But if items are identical, many states are equivalent, causing redundant exploration. However, with pruning by capacity, many of those states will lead to the same bin loads, but due to distinct items, DFS will still explore many duplicate states. In worst case, when capacities are large enough to hold all items together, for m=6, we will try to assign each item to any bin. The DFS tree will have depth 6, branching factor up to 6, so 6^6 = 46656 leaves. That's fine. For m=5, 5^6=15625. So total up to maybe ~ 100k, trivial.
Thus we can implement simple DFS.
Edge case: What if a and b are large such that only one item per bin? Then branching factor reduces because for each item, only bins that are empty (since any bin with an item would exceed capacity if we try to add second). Actually if only one item fits per bin, then the number of assignments is like permutations: first item can go to any of m bins, second to any of remaining m-1 (since putting in a bin with an item would exceed capacity, so only empty bins are allowed). The DFS will prune because bins[i]+item > n for non-empty bins. So branching factor reduces. So even better.
Thus simple DFS is fine.
Now implement solve() accordingly.
Let's test with provided examples manually.
Example 2: n=5, a=3, b=4. Items: [4,4,3,3,3,3] after descending sort: [4,4,3,3,3,3]. For m=4, DFS attempts. But is it possible to pack into 4 bins? Let's simulate quickly: Bin capacity 5. Each bin can hold at most one item, because any two items sum >= 3+3=6>5. So we need at least 6 bins. DFS will try assignments, but will eventually fail for m=4. It will explore many assignments, but due to pruning, it might still be okay. With m=4, number of bins=4, items=6. Since each bin can hold at most one item, we need to assign 6 items to 4 bins, which is impossible because each bin can hold only one. DFS will try to put first item in any of 4 bins, second item in any of the remaining 3 empty bins (since adding to a non-empty bin would exceed capacity), third item will have no empty bins that can accept it (all bins have some item, each item size 3 or 4, but adding a 3 to a bin that already has a 4 yields 7>5, similarly 4+3>5, 3+3>5). So third item cannot be placed, DFS backtracks. The total number of explored states: first level: 4 choices. For each, second level: up to 3 choices (empty bins). So at most 4*3=12. Third level: no placements, so total states ~12. So DFS is fast.
Thus overall algorithm is fine.
One more check: In Example 4, n=20, a=5, b=6. Items: [6,6,5,5,5,5] sorted descending? Actually largest first: 6,6,5,5,5,5. For m=2, we need to see if DFS finds solution. First item (6) can go to bin0 or bin1. Suppose bin0 gets 6. Second item (6) can go to bin0? 6+6=12 ≤20, so possible; also bin1. DFS will try both. One successful assignment: bin0:6+5+5+... Actually we need total 32, both bins ≤20. A solution: bin0: 5+5+6=16; bin1: 5+5+6=16. DFS will eventually find. Branching: first item 6 to bin0. Second item 6: try bin0 => sum 12; then third item 5: bin0 can take 5 => 17; fourth item 5: bin0 can take 5 => 22 >20, so can't; try bin1 (empty) => bin1=5; etc. Eventually may find solution. Complexity moderate.
Thus implementation should work.
Now, check potential pitfalls: Input reading: there are three lines each containing an integer. We'll use int(input()).
Now, edge case: n=1, a=1, b=1. Then total S=4+2=6 >1, so m>1. Lower bound ceil(6/1)=6. Each bin capacity 1, each item size 1, so 6 bins needed. DFS for m=5 will fail because we need to pack 6 items into 5 bins each capacity 1; each bin can hold exactly one item, so 5 bins can hold only 5 items, so impossible. So answer 6. Works.
Another edge: n=1000, a=1000, b=1000. Then each item size 1000, but we have 4 a and 2 b, each size 1000. Each bar can hold at most one item because two items sum to 2000>1000. So we need 6 bins. Lower bound ceil(6000/1000)=6. So answer 6.
Another: n=10, a=4, b=3. Items: four 4's, two 3's. Total S=44+23=16+6=22. Lower bound ceil(22/10)=3. Check m=3: capacity total 30, need to pack into 3 bins each ≤10. Is it possible? Let's try: we need to allocate items. Possible bins: Since 4+3+3=10, we can have one bin with 4+3+3=10. That uses one 4 and two 3's. Then we have three 4's left (since we used one 4). Each 4 is size 4, can we pack three 4's into two bins each ≤10? 4+4=8, plus another 4 would be 12>10, so we need three bins for three 4's? Actually we have two bins left. One bin can hold up to two 4's (8), the other can hold one 4 (4). So total bins used: first bin (4+3+3) + two bins for the remaining three 4's = 3 bins? Wait, we used three bins total: bin1: 4+3+3=10; bin2: 4+4=8; bin3: 4=4. That's 3 bins, all ≤10. So it works. So answer 3. Lower bound matches.
Another: n=7, a=3, b=3. Items: four 3's, two 3's -> total 6 items all size 3. S=18. Lower bound ceil(18/7)=3 (since 3*7=21). Check m=3: capacity each 7, each bin can hold at most two 3's? 3+3=6 ≤7, so two per bin. With 6 items, we need exactly 3 bins each with two 3's (6). So answer 3.
Another: n=7, a=4, b=3. Items: four 4's, two 3's. S=44+23=16+6=22. Lower bound ceil(22/7)=4 (since 4*7=28). Check m=4: capacity total 28, need to pack 6 items into 4 bins each ≤7. What are possible? 4+3=7. So we can pair each 3 with a 4. That uses two bins: each bin (4+3)=7. Then we have two remaining 4's. Each 4 alone fits in a bin (size 4). That's 4 bins total. So answer 4. Check m=3: capacity total 21 <22, impossible. So answer 4.
Now, we could also attempt m=3 if we try to pack differently? Total capacity 21 <22, so impossible. So answer 4.
All good.
Thus DFS approach works.
Now I'll implement in Python.
Pseudo:
def can_pack(m, n, a, b):
items = [a]*4 + [b]*2
# optional: sort descending to improve pruning
items.sort(reverse=True)
bins = [0]*m
code
def dfs(idx):
if idx == len(items):
return True
for i in range(m):
if bins[i] + items[idx] <= n:
bins[i] += items[idx]
if dfs(idx+1):
return True
bins[i] -= items[idx]
return False
return dfs(0)
But note that Python's recursion depth is only 6, so fine.
Now, we need to call for m from 1 to 6.
But we can also compute lower bound L = (4a + 2b + n - 1)//n, and start from L. For safety, if L>6? L max = 6, but if n=1, S=6, L=6. So start from max(1, L) to 6.
That reduces checks. Let's do:
S = 4a + 2b
L = (S + n - 1) // n
for m in range(L, 7):
if can_pack(m):
print(m)
break
But we must ensure that for some cases, even if L=1, it's correct: if total fits, we can put all in one bar? But need to also check that 4a+2b ≤ n. If L=1, then S ≤ n, so one bar works because we can cut a single bar into all pieces? However, we need to ensure we can physically cut a bar of length n into pieces of sizes a and b such that total length exactly S? Since we can cut arbitrarily, we can cut off pieces of required lengths; the sum of lengths of pieces cannot exceed n. If S = n, we can cut the bar into the six pieces, leaving no waste. But can we cut a bar into 6 pieces? Yes, we can make cuts anywhere. So as long as sum ≤ n, we can cut out those pieces sequentially. So one bar is enough. So L=1 works.
Thus the loop is fine.
Now I'll implement.
Testing with examples:
Example 1: n=8, a=1, b=2 => S=8, L=1, can_pack(1) returns True? In can_pack, items sum 8, bins[0] will accumulate items one by one; at each step, bins[0]+item ≤8, so it will accept all. So returns True. Print 1.
Example 2: n=5, a=3, b=4 => S=20, L=4. Check m=4: can_pack(4) -> should return False. m=5 -> should return False. m=6 -> True. So print 6.
Example 3: n=6, a=4, b=2 => S=20, L=4 (since 20/6=3.33->4). Check m=4: returns True? Should. So print 4.
Example 4: n=20, a=5, b=6 => S=32, L=2 (since 32/20=1.6->2). Check m=2: returns True. So print 2.
All good.
But careful: For some cases, L might be 2, but packing into 2 may be impossible, so we go to 3. That's handled.
Now, we must ensure that can_pack function uses the same n, a, b as outer. We'll pass n as argument or closure.
Now, implement.
Let's test with some additional cases:
Case: n=10, a=4, b=3 (S=22, L=3). m=3? Check packing: as earlier, we found possible? Let's verify: Items: 4,4,4,4,3,3. Need to pack into 3 bins capacity 10. One possible: bin1: 4+3+3=10; bin2: 4+4=8; bin3: 4=4. That works. So can_pack(3) should return True. So answer 3. Good.
Case: n=9, a=4, b=3 (S=22, L=3 because 22/9=2.44->3). Check m=3: capacity each 9. Is it possible? Items: 4,4,4,4,3,3. Try to pack: 4+3+? 4+3=7, could add another 3? 4+3+3=10>9. So max sum per bin with combination of items? 4+4=8, 4+3=7, 3+3=6, single 4 or 3. We need to fit total 22 into 3 bins, each ≤9. Max total capacity 27. Could we? Let's attempt: If one bin has 4+4=8 (two 4s), remaining items: two 4s and two 3s total = 4+4+3+3=14 into two bins each ≤9. Could we split 14 into two ≤9? Need one bin ≤9, other ≤9, but both sum 14, so possible if each ≤9 and at least 5 each (since 14-9=5). Could do 8 and 6? For example, bin2: 4+3=7, bin3: 4+3=7? That sums 14, but 7+7=14, both ≤9, okay. But careful: after using two 4s in bin1, we have two 4s and two 3s left. Bin2: 4+3=7, bin3: 4+3=7. That works. So total bins 3: bin1: 4+4=8, bin2:4+3=7, bin3:4+3=7. All ≤9. So answer 3. So L=3 works. Good.
Case: n=9, a=5, b=4. S=45+24=20+8=28. L= ceil(28/9)=4. Check m=4: capacity each 9. Items: five? Actually four 5's, two 4's. 5+4=9, so we can pair each 4 with a 5: two bins (5+4)=9 each. Then two remaining 5's: each alone in a bin (5 ≤9). That's 4 bins. So answer 4.
Case: n=9, a=5, b=5. Items: four 5's, two 5's? Actually b=5, so total items: 6 fives. S=30, L=4 (30/9=3.33->4). Capacity each 9, each bin can hold at most one 5 (since two fives sum 10>9). So we need 6 bins, not 4. So answer 6. Lower bound 4, but actual 6. So our loop will check m=4,5,6. For m=4, can_pack(4) should return False because we cannot pack 6 items of size 5 into 4 bins of capacity 9 (max items per bin=1, so max total items=4). So it fails. m=5: max items=5, still fails. m=6: success. So answer 6.
Thus algorithm handles that.
Now, is there any scenario where answer is 5? Possibly if combination yields that 5 bins are enough but not 4. Example: n=9, a=5, b=4 gave 4 bins. Need a case where total items 6, capacities such that each bin can hold at most 1 item except some bins can hold 2? Actually if some bins can hold 2 items and some only 1, we might need exactly 5. For instance, if we have items of sizes that sometimes can be paired, but the pairing constraints lead to needing 5 bins. Let's construct: total items 6. Suppose we have 4 items of size 4, and 2 items of size 3. n=7? Actually earlier n=7, a=4,b=3 gave 4 bins. Not 5. To get 5, we need that the best packing uses 5 bins, i.e., we cannot pack into 4 bins, but can into 5. With 6 items, that means the maximum number of items per bin is limited, and the best packing into 4 bins cannot accommodate 6 items. That would require that each of the 4 bins can hold at most 1 item? Then max items=4 <6, so need at least 6? Wait if each bin can hold at most 1, then need 6 bins. So 5 bins would also be insufficient if max items per bin is 1, because 5 bins can hold at most 5 items. So for 5 bins to be sufficient, we need that the total capacity of 5 bins is enough to hold all items, and also there exists a packing where the sum of items in each bin ≤ n. But if 4 bins cannot hold them, but 5 can, then the packing must involve at least one bin holding 2 items, because if all bins held at most 1, then 5 bins would hold at most 5 items, insufficient. So at least one bin holds 2 items. That's plausible. So 5 could be answer. Example: n=7, items: let's try to force 5 bins. We have 4 a's and 2 b's. Suppose a=3, b=5? But b ≤ n, so b=5. Then items: four 3's, two 5's. n=7. Total S=43+25=12+10=22. Lower bound ceil(22/7)=4. Check m=4: capacity total 28, but can we pack? Let's try: 5+3=8 >7? Actually 5+3=8 >7, so 5 cannot be paired with 3. 3+3=6 ≤7, so two 3's can go together. 5 alone fits. So we have two 5's each need own bin (since 5+anything >7 except maybe 2, but we have only 3's). So that uses 2 bins for the 5's. Then we have four 3's to pack into remaining bins. With two bins left (if total 4 bins), we need to pack four 3's into two bins, each can hold at most two 3's (3+3=6). So that's exactly two bins with two 3's each. So total bins: 2 (for 5's) + 2 (for 3's) =4. So m=4 works. So not 5.
Try to make m=4 impossible but m=5 possible. Need a scenario where the two b's cannot be paired with a's, and a's cannot be fully paired among themselves due to capacity, but with an extra bin we can accommodate. For m=4, we have 4 bins. Suppose we have items: a=4, b=4? Then all items 4, n=7. Each bin can hold at most one 4 (since two 4's=8>7). So need 6 bins. So m=5 insufficient as well (max items 5). So m=5 not enough; need 6. So that gives 6, not 5.
Maybe with a=5, b=3, n=7? a=5, b=3. Items: four 5's, two 3's. n=7: 5+3=8>7, so cannot pair. 5 alone fits, 3 alone fits, but 3+3=6 ≤7, so two 3's can be together. So we have four 5's each need own bin → 4 bins. And two 3's can go together in one bin, but that would be a 5th bin? Actually we have 4 bins used for 5's, we still have two 3's. They could be placed together in one additional bin, making total 5 bins. Could we place a 3 into a bin that already has a 5? No, because 5+3=8>7. So each 5 must be alone. So total bins = 4 (for 5's) + 1 (for two 3's) =5. So m=5 works. Can we do with 4 bins? If we have 4 bins, we need to place four 5's and two 3's. Since each bin can hold at most one 5 (if we put a 5, cannot add 3), the maximum number of 5's we can place in 4 bins is 4 (one per bin). After placing 5's in all 4 bins, there is no bin left for the 3's because adding a 3 to any bin with a 5 exceeds capacity. So we need at least one more bin for the 3's. So m=4 fails. Thus answer would be 5. Check lower bound: S=45+23=20+6=26. n=7, ceil(26/7)=4 (since 4*7=28). So lower bound 4, but actual needed 5. So this is a case where answer is 5. Let's test with our algorithm: n=7, a=5, b=3. Items: [5,5,5,5,3,3]. m=4: should be false, m=5: true. So answer 5.
Thus our algorithm must correctly handle answer 5.
Now, verify with DFS: For m=4, items sizes: five? Actually four 5's and two 3's. DFS will attempt assignments. It will likely fail as described. For m=5, should succeed.
Now, check if any scenario yields answer 5? Yes, we found one.
Thus algorithm will iterate m from L=4 to 6, and find that m=4 fails, m=5 succeeds, output 5.
Thus all good.
Now implement.
Potential improvements: Since we are using recursion with list of items, we might want to sort items descending to fill large items first, which helps prune.
We also need to consider that items are integers, but sizes may be up to 1000, but that's fine.
Now, we should also consider that sometimes a bar may be used to produce a side that is exactly length n, i.e., a or b could equal n. That's fine.
Now, possible pitfalls: In DFS, we assign each item individually, but if there are multiple items of same size, we might explore many duplicate states. However, as argued, total states are limited. But to be safe, we can use memoization with state representation (sorted tuple of bin loads) to avoid duplicates. But that adds complexity. Since maximum states is small, we can skip.
But consider worst-case: n=1000, a=1, b=1, m=6. Then items: six 1's. The DFS will try to assign each item to any of the 6 bins. The number of assignments of 6 distinct items to 6 bins is 6^6 = 46656. Each call does a loop over 6 bins, so total operations maybe ~ 6^6 * 6 = 279936, which is fine. For m=5, 5^6=15625, etc. So total for all m from 1 to 6, worst-case sum of m^6 for m=1..6? Actually we start from L, but L may be 1 if total sum small. But worst-case could be when L=1 but m=1 works, we only check m=1. That's trivial. The worst-case number of states explored is when we need to check multiple m values, and each m has high branching. But even if we check m=2,3,4,5,6 for a case with many possibilities, total states may be up to 2^6+3^6+4^6+5^6+6^6 ≈ 64+729+4096+15625+46656 = 67170, plus some overhead. That's still trivial for Python.
Thus simple DFS is absolutely fine.
One more nuance: In can_pack, we are not reusing bars? We allocate bins as we go. That's fine.
Now, implement solve().
But I realize: The problem might be from Codeforces or similar. Usually constraints are small, and solution is simple brute force or math.
Alternatively, we could derive formula by analyzing possibilities for number of bars: 1,2,3,4,5,6 and conditions. But brute force is easier.
Thus I'll write code:
import sys
def can_pack(n, a, b, m):
items = [a]*4 + [b]*2
items.sort(reverse=True) # not necessary but helps
bins = [0]*m
code
def dfs(idx):
if idx == len(items):
return True
# optionally, we can skip bins that are same as previous to avoid duplicates?
for i in range(m):
if bins[i] + items[idx] <= n:
bins[i] += items[idx]
if dfs(idx+1):
return True
bins[i] -= items[idx]
return False
return dfs(0)
def solve():
import sys
data = sys.stdin.read().strip().split()
if not data:
return
n = int(data[0])
a = int(data[1])
b = int(data[2])
S = 4a + 2b
L = (S + n - 1)//n
for m in range(L, 7): # max needed 6
if can_pack(n, a, b, m):
print(m)
break
if name == "main":
solve()
But we need to be careful: The input is three lines each with integer, so reading all is fine.
Now test with custom cases.
Let's run mental test for n=7, a=5, b=3.
S=26, L=4.
can_pack(4): items sorted descending: [5,5,5,5,3,3]? Actually descending: all 5's then 3's. So first item 5 placed in some bin. We'll not predict, but DFS will explore. Eventually should return False.
can_pack(5): should return True.
Now, we should test with n=7, a=5, b=3 manually.
But we might also test with n=8, a=5, b=3: S=45+23=20+6=26, L=4 (since 26/8=3.25->4). Could we pack into 4 bins? Let's see: Items: four 5's, two 3's. n=8. Can we pair 5+3=8? Yes! So each 3 can go with a 5, using two bins: (5+3)=8 each. Then we have two remaining 5's, each alone in a bin (5≤8). That's total 4 bins. So answer 4. Check if 3 bins possible? Total capacity 24 <26, impossible. So answer 4. L=4 works.
Now, n=8, a=5, b=4: S=45+24=20+8=28, L=4 (28/8=3.5->4). Check m=4: 5+4=9 >8, so cannot pair. 5 alone fits, 4 alone fits, 4+4=8, 5+? cannot. So we need: four 5's need own bins (since can't pair with 4). That's 4 bins for the 5's. Then we have two 4's: they could be together in one bin? 4+4=8 ≤8, so they can share one bin. But that would require a 5th bin because the 5's already occupy 4 bins and we can't add 4's to those bins (5+4=9>8). So total bins needed = 5. But capacity total for 5 bins =40, S=28. So L=4 but actual 5. Let's see if there is any way to pack into 4 bins? With 4 bins, each capacity 8, we have 6 items. Each bin can hold at most one 5 (since two 5's would be 10>8). So max number of 5's we can place in 4 bins is 4, that's okay. But after placing four 5's in each of the 4 bins, each bin has 5, leaving capacity 3. Can we place a 4 into any of these bins? 5+4=9>8, so no. So the 4's must go into bins, but all bins are occupied with 5's and cannot accept 4's. So we need additional bins for the 4's. So at least 5 bins. So answer 5. So this is another case where answer 5.
Thus our algorithm should return 5.
Now test with DFS: For m=4, it should fail; for m=5 succeed.
Thus fine.
Now consider n=9, a=5, b=4: earlier we had answer 4 because 5+4=9, so two bins for pairs, and two remaining 5's each alone → total 4. Good.
Now consider n=9, a=5, b=5 gave answer 6.
Now consider n=9, a=4, b=5: same as a=5,b=4? Actually symmetric: 4+5=9, still works. But we have four a's (4) and two b's (5). So two bins: (4+5) each, uses two 4's and two 5's. Then two remaining 4's need two bins → total 4. So answer 4.
Now consider n=6, a=4, b=3: S=44+23=16+6=22, L=4 (22/6=3.66->4). Items: four 4's, two 3's. n=6. 4+3=7>6, so cannot pair. 3+3=6, 4 alone fits. So we need: two 3's can be together in one bin (3+3=6). Four 4's need four bins (each 4 fits). So total 5 bins? Wait, that would be 1 bin for the two 3's, plus 4 bins for the 4's = 5 bins. But total capacity 5*6=30, S=22. Can we do with 4 bins? With 4 bins, each capacity 6, we need to place 6 items. Each bin can hold at most one 4 (since 4+4=8>6). So max 4's we can place in 4 bins is 4 (one per bin). That accounts for all 4's. Then we have two 3's. Could they be placed into bins that already have a 4? 4+3=7>6, so no. So they need their own bins, but all bins are occupied by 4's. So we need at least 6 bins? Wait, we have 4 bins each containing a 4. To place the 3's, we would need two more bins, total 6. But earlier we thought we could put the two 3's together in one bin (3+3=6). So if we have 4 bins for 4's and 1 bin for 3's, that's total 5 bins. But we only have 4 bins? That scenario uses 5 bins, not 4. So with 4 bins, we cannot place both 3's because each bin can only hold one 4 (and cannot also hold a 3). So after placing 4's, we have 0 bins left for the 3's. So we need additional bins. So minimum bins = 5? But is it possible to have a bin that contains no 4 but two 3's? That would be an extra bin. So total bins = bins containing 4's (4 bins) + bin containing two 3's (1 bin) =5. But could we have a bin that contains one 4 and one 3? No, because 4+3=7>6. Could a bin contain two 4's? No. Could a bin contain one 3 only and the other 3 with another 4? No. So indeed we need at least 5 bins. But wait, we have 4 bins for the 4's, but maybe we could put two 4's in one bin if we don't use all 4 bins for 4's? Actually we have 4 items of size 4. To reduce number of bins, we might try to put two 4's in a single bin? 4+4=8>6, so impossible. So each 4 must occupy its own bin. So we need at least 4 bins for the 4's. Then we have two 3's, which could be placed together in one bin (since 3+3=6). So that adds one more bin, total 5. Could we place one of the 3's with a 4? No. So answer should be 5. But let's compute S=22, n=6, lower bound ceil(22/6)=4. But actual needed is 5. So that's another case for answer 5. Check with our algorithm: n=6, a=4, b=3. Items: [4,4,4,4,3,3]. For m=4, should fail. m=5, should succeed. So answer 5.
But earlier we had example 3: n=6, a=4, b=2 gave answer 4. So b=3 changes.
Thus our algorithm must handle answer 5.
Now, test with n=6, a=4, b=3 manually in DFS.
Now, also consider n=6, a=4, b=1? a=4,b=1 gives items: four 4's, two 1's. n=6: 4+1=5 ≤6, so we can pair each 1 with a 4, perhaps. We have 4 fours and 2 ones. So we can have two bins with 4+1=5, and two bins with just 4 (4). That's 4 bins. So answer 4. So b matters.
Thus, our algorithm should be correct.
Now, let's also consider the scenario where a and b might be zero? Not allowed.
Thus, final solution is as described.
Now, we need to ensure that we handle the case where n is very small but a,b are 1? Already fine.
One more nuance: The problem statement says "Petya has equal wooden bars of length n. He wants to make a frame for two equal doors. Each frame has two vertical (left and right) sides of length a and one top side of length b." It doesn't mention bottom side, so indeed three sides per door.
Thus final answer.
Potential pitfalls: The input may have extra spaces or lines, but reading via sys.stdin.read().split() handles that.
Now, I'll code.
But I recall that the problem might be from Codeforces Round? Possibly there is a more clever solution, but brute force is acceptable.
Now, think about possible optimization: We can also compute answer by checking a set of conditions. However, given we have only 6 items, brute force is simplest.
Now, I'll write the final solution.
But before finalizing, double-check that our DFS considers that we can cut bars into pieces that are not necessarily exactly the items? But we are assigning each item to a bar, which corresponds to cutting that bar to produce that piece. That's valid because the sum of assigned items for a bar is ≤ n, we can cut the bar to get those pieces. So yes.
Now, note: The problem says "solid (i.e. continuous without breaks) piece of bar is needed for each side." So each side must be a single piece. That's exactly our items.
Now, one more nuance: Could we use a bar to produce more than one side for different doors? Yes, we are packing all pieces into bars.
Thus algorithm is correct.
Now, implement.
But there is a subtlety: In our DFS, we treat each item as distinct. However, when we assign items to bins, we could assign the same type items in any order, but that may cause many duplicate states. But as we argued, it's fine. However, for the worst-case of all 1's, m=6, we might explore 6^6=46656 leaves. But that's fine. However, consider that recursion might revisit the same state multiple times due to different orderings of items. But since we are assigning items sequentially, each leaf corresponds to a specific assignment of each distinct item to a bin. For identical items, many permutations produce the same bin loads, but we still explore all. 46656 is still fine. However, if we are concerned about time limit (usually Codeforces time limit 1 sec, Python 46656 operations is trivial). But we might also have to test multiple test cases? The problem is a single test case. So okay.
But for completeness, we could implement a more efficient state search using DP with counts. For example, we could use recursion over bins: for each bin, choose a subset of remaining items that fit, and proceed. But that's more complex.
Thus final answer.
I'll produce the code.
But before final, test on some random values to ensure no infinite recursion or errors.
I'll simulate in mind for n=1000, a=1000, b=1000: items all 1000. For m=6, DFS: first item 1000 placed in bin0; second item 1000 can be placed in bin0? 1000+1000=2000>1000, so not allowed; so it will go to bin1; etc. So it will assign each item to a distinct bin because no two fit. The number of assignments: first item 6 choices (any bin), second item 5 choices (any bin except the one with first item because capacity exceeded), third 4, etc. So number of leaves = 6! = 720, not 6^6. So even less.
For a case where all items are size 1 and n is large, bins can hold many items. For m=6, items 6, each can go to any bin; there are 6^6 assignments. But that's maximum. That's fine.
Now, what about m=5 with all 1's? 5^6=15625. Fine.
Thus solution will run quickly.
Now, ensure that we break early when found solution.
Now, consider reading input: The problem states first line n, second line a, third line b. So we can read line by line: n=int(input()), etc. That is simpler.
I'll use:
def solve():
import sys
n = int(sys.stdin.readline())
a = int(sys.stdin.readline())
b = int(sys.stdin.readline())
...
But careful: There might be trailing spaces, but int() handles.
Now, implement can_pack.
One more micro-optimization: In can_pack, we can sort items descending so that large items are placed first, reducing branching. That's good.
I'll write:
items = [a]*4 + [b]*2
items.sort(reverse=True)
Now, in DFS, we may also sort bins? Not needed.
Now, note: In recursion, we use closure over n, items, bins, m. All good.
Now, we also might want to add an early check: if any single item > n, but problem says a,b ≤ n, so not needed.
def dfs(idx):
if idx == len(items):
return True
for i in range(m):
if bins[i] + items[idx] <= n:
bins[i] += items[idx]
if dfs(idx+1):
return True
bins[i] -= items[idx]
return False
return dfs(0)
def main():
data = sys.stdin.read().strip().split()
if not data:
return
n = int(data[0])
a = int(data[1])
b = int(data[2])
total = 4a + 2b
L = (total + n - 1) // n
for m in range(L, 7):
if can_pack(n, a, b, m):
print(m)
return
if name == "main":
main()
But we need to ensure that can_pack uses the correct n, a, b. We pass n, a, b.
But a and b are not used inside can_pack except to construct items. So we could just pass items list, but okay.
Now, we might want to add a check for m=1 first, as L could be 1. Works.
Now, test with the provided examples:
Example 1: Input:
8
1
2
Our code: n=8, a=1, b=2, total=8, L=1, for m=1: can_pack? items = [2,2,1,1,1,1] after sort descending? Actually items = [1]*4+[2]*2 = [1,1,1,1,2,2]; sort reverse => [2,2,1,1,1,1]. DFS: bins[0] initially 0. Place first 2: okay, bin0=2. second 2: try bin0 (2+2=4≤8) => ok, bin0=4. third 1: bin0=5, fourth 1: bin0=6, fifth 1: bin0=7, sixth 1: bin0=8. All fit. So returns True. Print 1.
Example 2: 5,3,4 => total=20 L=4. m=4: items = [4,4,3,3,3,3] after sort: [4,4,3,3,3,3]? Actually sort descending yields [4,4,3,3,3,3]. DFS for m=4: we need to see if it can pack. Should return False. m=5: also false, m=6 true. So output 6.
Example 3: 6,4,2 => total=20 L=4, m=4 returns True? items: [4,4,4,4,2,2] sorted [4,4,4,4,2,2]. Try packing: one possible: bin0:4+2=6, bin1:4+2=6, bin2:4, bin3:4. DFS should find. So output 4.
Now, test with n=7,a=5,b=3 (answer 5). total=26 L=4. m=4 should fail, m=5 should succeed. Our DFS for m=4 will attempt to pack. Let's simulate if it can ever succeed? Probably not. So output 5.
Now, test with n=6,a=4,b=3 (answer 5). total=22 L=4. m=4 should fail, m=5 succeed. Good.
Now, test with n=9,a=5,b=5 (answer 6). total=30 L=4. m=4 false, m=5 false? Actually with n=9, each bin can hold at most one 5, so 5 bins can hold at most 5 items, we have 6 items, so m=5 false, m=6 true. So output 6.
Now, test with n=8,a=5,b=4 (answer 5). total=28 L=4. m=4 false, m=5 true. Good.
All good.
Now, let's also test with n=1000, a=500, b=501. Items: four 500, two 501. n=1000. Can we pack into 3 bins? total=4500+2501=2000+1002=3002. Lower bound ceil(3002/1000)=4. Could 3 bins work? total capacity 3000 < 3002, impossible. So m=4 needed. But is packing into 4 possible? We need to see if any two items can fit in one bin: 500+500=1000 exactly, so two 500's can be together. 500+501=1001 >1000, so cannot pair 500 with 501. 501+501=1002 >1000, so two 501's cannot be together. So we have two 501's that must each be alone. So they need two bins. Then we have four 500's left. We can pair them into two bins of two 500's each (500+500=1000). So total bins = 2 (for 501's) + 2 (for pairs of 500) =4. So answer 4. Lower bound 4 matches. So m=4 works.
Now, test with n=1000, a=600, b=600. Items: four 600, two 600, total six 600's. Each bin can hold at most one 600, so need 6 bins. total sum 3600, lower bound ceil(3600/1000)=4. So answer 6.
Now, our algorithm for m=4: with items all 600, each bin capacity 1000, can we place more than one 600? 600+600=1200>1000, so only one per bin. So with 4 bins we can place at most 4 items, but we have 6 items. So m=4 fails. m=5 fails (max 5 items). m=6 works.
Thus answer 6.
Now, we need to ensure that DFS can handle m=6 with 6 items each size 600: The DFS will assign each item to a distinct bin, but it might also try to put two in same bin, but those will be rejected because sum >1000. So branching will be like: first item: 6 choices (any bin). second item: cannot go to the bin with first item, so 5 choices. So total leaves = 6! = 720. That's fine.
Thus all set.
Now, think about memory/recursion: depth 6, fine.
One potential improvement: We could use itertools.product? Not needed.
Thus final answer.
Now, produce final solution explanation and code.
But I recall that some solutions on Codeforces for this problem used a different approach: They considered all ways to allocate items to at most 6 bars, by iterating over the number of bars used and checking if a certain inequality holds. Actually there might be a more mathematical solution because the numbers are small, but we can also brute force all possible ways to cut a bar: i.e., for each bar, it can contain x vertical sides and y horizontal sides, where 0 ≤ x ≤ 4, 0 ≤ y ≤ 2, and xa + yb ≤ n. Then we want to choose some multiset of such patterns to cover the required counts. Since the total number of patterns is at most 15, we can try to cover using at most 6 bars. We could use DP over counts, but our DFS is fine.
But maybe there is a simpler greedy: If total length fits in one bar, answer 1. Else if we can put both horizontal sides (b) together? Not always.
Given the constraints, brute force is simplest and less error-prone.
I will produce final answer.
Now, check for potential bug: In can_pack, we sort items descending. But note that after sorting, items list is sorted, but we then call dfs(0) repeatedly. That's fine.
But one nuance: In DFS, we use the same items list for each call of can_pack. That's fine.
But what about the case where we have multiple items of same size? Sorting descending doesn't change.
Now, one more possible improvement: We could add a check: if m >= 6, return True because each item fits individually. Actually we can early return True for m=6 because we can always put each item in its own bar. But we are iterating from L upward, and L could be 6, so we will call can_pack(6). That will definitely return True. But we can just return True without DFS. We can add:
if m >= 6:
return True
Because with 6 bars, we can assign each of the 6 items to separate bars. But it's not always true if some item's length > n? But given a,b ≤ n, yes. So we can add that optimization. But not necessary.
I'll include it to make can_pack(6) trivial.
Also, we can add check for m=1: if total <= n, return True.
But our DFS will also handle it.
But to speed up and avoid DFS in trivial cases, we can add these early returns.
I'll implement:
def can_pack(n, a, b, m):
if m >= 6:
return True
total = 4a + 2b
if m == 1:
return total <= n
# else general case
items = [a]*4 + [b]*2
items.sort(reverse=True)
bins = [0]*m
code
def dfs(idx):
if idx == len(items):
return True
for i in range(m):
if bins[i] + items[idx] <= n:
bins[i] += items[idx]
if dfs(idx+1):
return True
bins[i] -= items[idx]
return False
return dfs(0)
But note: For m=1, our DFS also works, but we can short-circuit.
However, careful: For m=1, the DFS will simply place all items into bin0 if sum ≤ n, else fail. That's fine. But early returns may make code a bit faster but not needed.
But we should note that for m=1, items sorted descending, we will still loop over bin0 each time; but it's fine.
But the early return for m>=6: Actually if m==6, we can always assign each item to separate bars because each item length ≤ n. So we can return True. But is there any scenario where we have more than 6 items? No, exactly 6 items. So m=6 always works. So we can just break before calling can_pack if m==6, but we are iterating m from L to 6; if L=6 we will call can_pack(6) which we know is true. But we could just print 6 if L=6. However, we want to check for m=6 anyway, but we can just set answer=6 if loop reaches m=6 without need for can_pack. But our can_pack(6) will also succeed. But we can simplify by not calling can_pack for m=6 and just print 6 if we get there. But I think it's okay to call can_pack(6) with early return True.
Thus final code.
But note: In our loop, we break when can_pack returns True. For m=6, can_pack(6) will return True, so we print 6.
Thus all good.
Now, I'll produce final answer.
Let's also think about any hidden traps: The problem might require that we consider that the vertical sides are of length a, and there are two per door, so total 4, but also maybe the frame includes a bottom side? The problem statement says "two vertical (left and right) sides of length a and one top side of length b." It doesn't mention bottom, so we are correct.
Now, finalize.
Will there be any case where answer is 0? No, because at least one bar is needed (since a,b≥1, so there is at least one side).
Now, let's produce final answer with explanation.
I'll now write final output. ```python
import sys
def can_pack(n, a, b, m):
"""
Check if the 6 required pieces (4 of length a, 2 of length b)
can be cut from m bars of length n.
"""
# With 6 bars we can always put each piece in its own bar.
if m >= 6:
return True
code
# With one bar it is possible exactly when the total length fits.
if m == 1:
return 4 * a + 2 * b <= n
# General case: perform a depth‑first search.
pieces = [a] * 4 + [b] * 2
pieces.sort(reverse=True) # place larger pieces first – better pruning
bins = [0] * m # current load of each bar
def dfs(idx):
if idx == len(pieces):
return True
for i in range(m):
if bins[i] + pieces[idx] <= n:
bins[i] += pieces[idx]
if dfs(idx + 1):
return True
bins[i] -= pieces[idx]
return False
return dfs(0)
def main():
data = sys.stdin.read().strip().split()
if not data:
return
n = int(data[0])
a = int(data[1])
b = int(data[2])
code
total = 4 * a + 2 * b
lower = (total + n - 1) // n # ceil(total / n)
for m in range(lower, 7): # answer is at most 6
if can_pack(n, a, b, m):
print(m)
return