#K85212. Maximum Berries Collection
Maximum Berries Collection
Maximum Berries Collection
Farmer John owns several fields with berry bushes arranged in a linear fashion. Each field contains a certain number of berries. However, if he collects berries from two consecutive fields, he risks losing quality or alerting competitors.
Your task is to determine the maximum number of berries Farmer John can collect without picking from two adjacent fields. Formally, given an array \(fields\) of non-negative integers, where \(fields[i]\) represents the number of berries in the \(i\)-th field, compute the maximum sum of elements with the condition that no two selected elements are adjacent.
inputFormat
The input is read from stdin and consists of two lines:
- The first line contains an integer \(n\), the number of fields.
- The second line contains \(n\) space-separated integers, each representing the number of berries in a field.
outputFormat
Output a single integer to stdout representing the maximum number of berries that can be collected subject to the constraint.
## sample5
3 2 5 10 7
15