You are given carpets, numbered from to . Carpet has length .
You want to lay all the carpets along a straight corridor of the same width as the carpets. You may choose their positions and the order in which you lay them. Wherever carpets overlap, only the one laid last is visible from above.
Every carpet must lie entirely inside the corridor, without being cut or folded. Both endpoints of every carpet must be at integer distances from the left end of the corridor.
The carpets must cover the entire corridor. Viewed from above, the corridor must consist of exactly consecutive sections, each of length at least . In the -th section from the left, only carpet must be visible.
Find the minimum possible length of the corridor.
The first arrangement can expose carpets , , and on segments of lengths , , and , respectively. No corridor shorter than can accommodate this top view.
A corridor of length is sufficient for the second sequence, and it is impossible to use a shorter corridor because carpet itself has length .
A corridor of length can display the three carpets in the required order. In particular, placing the longest carpet at the bottom is not necessary.
The four carpets can be arranged so that the required top view has total length ; any shorter corridor cannot contain either carpet of length together with the required visible parts.