Minimum cost paper folding
A strip of paper has N squares in a row; square i weighs W[i]. You fold it along a line between two squares: the shorter side is lifted and laid over the longer side (if both sides are equal, the left side is lifted). A fold costs the total weight of the lifted side, and each square of the longer side then carries the weight that landed on it. Keep folding until the paper is a single stack one square wide. Print the minimum total cost.
Reported in: Infosys (SP / DSE · 29 Nov 2026) (memory-based)
Input
First line: N. Second line: N weights.
Output
The minimum total folding cost.
Constraints
1 ≤ N ≤ 14; 1 ≤ W[i] ≤ 1000.
Example 1
Input:
2
3 5
Output:
3Example 2
Input:
3
1 2 3
Output:
4Example 3
Input:
4
4 1 1 4
Output:
7Hint: Each fold leaves a shorter strip of stacked weights, and N is small. Try every fold line, recurse on the strip it leaves, and memoise the answer for each strip of weights you have already solved.