File Recovery
Time limit2sMemory limit512 MB
Delete elements from a sequence so the remainder parses as length-prefixed blocks that end exactly at the last position, minimizing the largest likelihood among deleted elements.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Binary search, Greedy, Prefix sum
- Solved
- No attempts yet
Problem
Company shake! represents each user's data as positive integers, converts each one into a data piece, concatenates the pieces, and saves the result in a file. A data piece starts with its length. If the length is , the piece consists of followed by numbers, and a piece with consists of the single number .
Suppose three users have data , , and . Their pieces are , , and , and the file stores the concatenation .
The damaged file may contain extra numbers that were not in the original file. For each position, an integer is given that represents the likelihood that the number was also in the original file. Delete numbers so that the rest forms a correct file while minimizing the maximum likelihood among the deleted numbers.
Input
The first line gives the count of numbers in the damaged file. It satisfies .
The second line gives integers forming the damaged file. Each is between and inclusive.
The third line gives integers representing the likelihood that each number was also in the original file. Each is between and inclusive.
Output
Print the minimized maximum likelihood among the deleted numbers.
If no deletion is needed, print .
Hint
To check the remaining sequence, scan from the front. If the first number of a piece is , the next numbers belong to the same piece, and the following piece starts right after them. The file is correct when the last piece ends exactly at the end of the sequence.
Deleting positions keeps the order of the remaining numbers, and the first number of each piece must equal the count of remaining numbers assigned to that piece.