#K68952. Maximum Gold Theft
Maximum Gold Theft
Maximum Gold Theft
You are given a row of (N) treasure chests, each containing a specific amount of gold. You can rob the chests based on a simple rule: you must choose to steal from either all the chests in even-numbered positions or all the chests in odd-numbered positions (using (0)-indexing). Your goal is to maximize the total amount of gold stolen.
Mathematically, if the chests are represented as an array (A) of length (N), you need to compute
[ \text{answer} = \max\left(\sum_{i=0}^{\lfloor \frac{N-1}{2} \rfloor} A[2i], \sum_{i=0}^{\lfloor \frac{N-2}{2} \rfloor} A[2i+1]\right) ]
Output the maximum obtainable gold.
inputFormat
The input consists of two lines:
- The first line contains a single integer (N), the number of chests.
- The second line contains (N) space-separated positive integers representing the amount of gold in each chest.
outputFormat
Output a single integer that represents the maximum amount of gold that can be stolen following the given rule.## sample
6
2 3 5 7 1 4
14