#C9355. Maximum Aesthetic Value

    ID: 53439 Type: Default 1000ms 256MiB

Maximum Aesthetic Value

Maximum Aesthetic Value

You are given a sequence of n buildings where each building i has a height \(h_i\) and an aesthetic value \(a_i\). Your task is to choose a continuous segment of these buildings such that the heights in the segment are non-decreasing (i.e. \(h_i \le h_{i+1}\) for every adjacent pair in the segment) and then compute the sum of the aesthetic values of that segment.

The goal is to find the segment with the maximum total aesthetic value. If all possible segments yield a negative sum, you should output 0.

Note: The segment must be contiguous. If the best contiguous segment has a sum less than or equal to 0, print 0.

inputFormat

The input is given via standard input (stdin) in the following format:

n
h1 h2 ... hn
a1 a2 ... an

Where:

  • n is the number of buildings.
  • The second line contains n space-separated integers representing the heights \(h_1, h_2, \ldots, h_n\).
  • The third line contains n space-separated integers representing the aesthetic values \(a_1, a_2, \ldots, a_n\).

outputFormat

Output a single integer to standard output (stdout): the maximum total aesthetic value of any contiguous segment with non-decreasing heights. If no such positive segment exists, output 0.

## sample
5
2 3 3 5 1
1 -2 3 4 0
6

</p>