Stacker

View as PDF

Points: 15 (partial)
Time limit: 1.0s
Memory limit: 64M

Authors:
Problem type

In this world, there are those who dread the life of working, and would much rather stay at home, living a life of a recluse, away from other people. Some dread social settings, and pessimistically view other people as superficial. Life is just another trash game to them, one that they have gradually grown tired of.

Little Nate, an eccentric shut-in who (by definition) stays at home most of the time, falls into the categories above. It is currently summer vacation for him, yet he remains cooped up at home, finding his own means to entertain himself. To him, summer vacation allows for a retreat from the hot weather outside, and he argues (to himself) that going out during summer vacation with friends only serves to expose oneself to the scorching heat of summer, and it hence defeats the purpose of summer vacation. For such a seemingly unmotivated person, he does find some things interesting, such as toys.

As Nate formulates the above arguments about summer vacation in his head while holding on to a stack of blocks that he plans to sell, he recalls that he has no friends, and his mind wanders back to 3 months ago, when he enlisted your help for packing boxes in boxes to troll his non-existent friends. Nate is shocked at his heavy blunder and fruitless efforts, falls into a momentary state of depression, and as a result, lost his balance of the blocks he was holding onto, causing the blocks to all fall onto the ground. Luckily for Nate, the blocks fell in a straight line, and in the same order as how he stacked them previously.

Having a far-above-average memory, Nate is able to recall the exact widths and heights of each of the blocks, and in the same order as they were on the ground. Every block has a square base and the th block from him (also th block from the left) has width and value . Due to his prolonged time living as a shut-in, Nate has developed one of the seven deadly sins – Sloth. He only wishes to walk from left to right once, without turning back. Furthermore, Nate wishes to only stack blocks above those with a smaller base area than itself for better stability, as he does not wish to commit the same mistake of dropping the blocks, hence Nate may pick up a block after block () if and only if . Nate also wishes to attain a maximum total value of the blocks from stacking through this one time walk, to gain as much money as possible from selling the blocks he picks up. Nate has a computer beside him, and has typed the necessary information regarding the boxes. Despite his overwhelming brilliance, Nate lacks knowledge of computer programming, and so once again he enlists your help to find how to maximise the total value of the blocks he may pick up, while satisfying his other conditions.

Input Specification

The first line of input will contain an integer .

The following two lines will each contain integers and respectively.

Output Specification

The output should contain exactly one line with one integer, the maximum value attainable if Nate were to pick up blocks strictly from left to right while satisfying his other conditions.

Constraints

for all , .

Sample test cases.

Sample Input

6
5 3 7 1 4 6
4 2 4 1 6 9

Sample Output

13

Explanation for Sample Output

Nate picks up blocks 3, 6 (as ) with a total value of , which is maximal.