#K81782. Warehouse Storage Optimization
Warehouse Storage Optimization
Warehouse Storage Optimization
You are given n storage units, each of which can store an item with a certain value. However, due to constraints, you cannot use two adjacent storage units at the same time. Your task is to determine the maximum total value of items that can be stored under these conditions.
Formally, if the values of the items are represented by an array \(a = [a_0, a_1, \dots, a_{n-1}]\), you need to choose a subset of these items such that no two chosen items are adjacent. The answer is the maximum sum of the selected items.
You can formulate the solution using the recurrence:
[ \text{dp}[0] = a_0,\quad \text{dp}[1] = \max(a_0, a_1)]
and for (i \ge 2):
[ \text{dp}[i] = \max(\text{dp}[i-1],; \text{dp}[i-2] + a_i)]
The final answer will be \(\text{dp}[n-1]\) (or 0 if \(n = 0\)).
inputFormat
The input is read from stdin and is formatted as follows:
- An integer \(n\) representing the number of storage units.
- A line containing \(n\) space-separated integers, where each integer represents the value of the item in that storage unit.
If \(n = 0\), the second line will be empty.
outputFormat
Output the maximum total value of items that can be stored, printed to stdout.
## sample5
3 2 5 10 7
15