#K44362. Maximum Sunlight Requirement

    ID: 27515 Type: Default 1000ms 256MiB

Maximum Sunlight Requirement

Maximum Sunlight Requirement

You are given n potted plants, each with a specific sunlight requirement. Your task is to find a continuous segment of these plants such that the sum of their sunlight requirements is maximized.

This problem is analogous to the well-known maximum subarray sum problem, which can be solved efficiently using Kadane's algorithm. In mathematical terms, if the sunlight requirements are represented as \(a_1, a_2, \dots, a_n\), you need to compute:

\[ current\_sum = \max(a_i, current\_sum + a_i) \quad \text{for each } i \] \[ max\_sum = \max(max\_sum, current\_sum) \]

Output the maximum sum obtained from any contiguous subarray.

inputFormat

The input is read from stdin and consists of:

  • The first line contains an integer n representing the number of plants.
  • The second line contains n space-separated integers, where each integer represents the sunlight requirement of a plant.

outputFormat

Output a single integer to stdout, which is the maximum possible total sunlight requirement for any contiguous segment of the plants.

## sample
4
1 2 3 4
10