Balloon Darts
InterviewTime limit1sMemory limit512 MB
Given balloon scores and a burst order, find the highest segment score recorded at any point while the balloons are burst.
- Level
Medium5 of 10
- Topics
- Union-find, Implementation
- Solved
- No attempts yet
Problem
A lonely coder named Byeongwoo was set up on a date by his friends, and the two went to an amusement park.
While chatting on a bench, Byeongwoo noticed a balloon dart game across the way. He wanted a high score to win a teddy bear, so he walked over to play. The park's game followed rules unlike any ordinary balloon dart game he knew.
-
The balloons are arranged in a single line, like a one-dimensional array.
-
There are as many darts as balloons. Byeongwoo is a dart master, so every dart he throws hits its balloon exactly and bursts it.
-
The score is the sum, over every segment, of (the sum of the balloon scores in the segment) times (the size of the segment). A segment is a contiguous subarray. More precisely, for an interval where all balloons in are still present, and balloon and balloon either do not exist or have been burst, the segment is the subarray of the full array . The score is given by the formula below.
- is the sum of the balloon scores in the -th segment.
- is the size of the interval of the -th segment.
- is the total number of segments.
- The score starts at 0 and is recalculated each time a dart bursts a balloon. The final score is the highest score recorded. Before the first dart is thrown, no score has been calculated, so the score is 0.
Input
The first line contains the number of balloons, an integer ().
The second line contains integers (), the scores of the balloons.
The third line contains distinct integers (), the order in which the balloons are burst.
Output
Print the final score.