#P1901. Maximum Received Energy

    ID: 15184 Type: Default 1000ms 256MiB

Maximum Received Energy

Maximum Received Energy

There are N energy transmitters arranged in a line. Each transmitter i has a unique height \(H_i\) and emits energy of value \(V_i\) simultaneously in both directions (transmitters at the ends emit only in one direction). The energy emitted by transmitter i is received only by the nearest transmitter on each side whose height is greater than \(H_i\). In other words, for each transmitter i, let:

  • \(L_i\) be the nearest transmitter to the left with \(H_{L_i} > H_i\) (if any).
  • \(R_i\) be the nearest transmitter to the right with \(H_{R_i} > H_i\) (if any).

Then transmitter \(L_i\) (if it exists) receives \(V_i\) from the left, and transmitter \(R_i\) (if it exists) receives \(V_i\) from the right. Note that one transmitter may receive energy from zero, one, or two other transmitters.

Your task is to calculate the maximum total energy received by any single transmitter.

inputFormat

The input consists of three lines:

  1. The first line contains an integer \(N\) representing the number of transmitters.
  2. The second line contains \(N\) space-separated integers denoting the heights \(H_1, H_2, \dots, H_N\) of the transmitters. All heights are distinct.
  3. The third line contains \(N\) space-separated integers denoting the energy values \(V_1, V_2, \dots, V_N\) emitted by the transmitters.

outputFormat

Output a single integer: the maximum total energy received by any transmitter.

sample

5
3 1 4 2 5
10 20 30 40 50
70