#C3093. Maximum Energy Collected in a Circular Garden Loop
Maximum Energy Collected in a Circular Garden Loop
Maximum Energy Collected in a Circular Garden Loop
You are given an integer n and a sequence of n integers energies representing the energy values of gardens arranged in a circle. A traveler can start at any garden, traverse n consecutive gardens (wrapping around the circle) and finally return to the starting garden. The task is to compute the maximum total energy that can be collected by choosing an optimal starting point.
In other words, if the gardens are labeled from 0 to n-1 and the energy in the ith garden is denoted by Ei, then one must determine
[ \max_{0 \leq i < n} \Bigg{ \sum_{j=0}^{n-1} E_{(i+j) % n} \Bigg} ]
Note: Even though every cycle covers all the gardens, the ordering may affect the energy collection when the energies include negative values. All input values are provided via standard input and the answer should be printed to standard output.
inputFormat
The input is given in two lines:
- The first line contains an integer n, representing the number of gardens.
- The second line contains n space-separated integers, where the ith integer represents the energy value Ei of the garden.
All input is read from stdin
.
outputFormat
Output a single integer, which is the maximum energy that can be collected by traversing all the gardens in the circle from an optimal starting point. The output should be printed to stdout
.
5
5 3 7 2 9
26
</p>