Two Towers
InterviewTime limit1sMemory limit128 MB
Remove the fewest blocks from the tops of two towers so the remaining towers have equal height.
- Level
Easy3 of 10
- Topics
- Greedy, Two pointers, Prefix sum
- Solved
- No attempts yet
Problem
Little Bajtek received a set of blocks from his grandfather. Each block has a certain height. Bajtek stacks the blocks one on top of another to form a tower. Using all of his blocks, he built two towers.
Now he wonders what is the minimum number of blocks he has to remove so that both towers have the same height. Blocks may be removed only from the top of each tower, and no new blocks may be added. In particular, it is allowed to remove all of the blocks from a tower. If every block is removed from both towers, both heights become 0 and are therefore equal.
Input
The first line contains two integers , (), the number of blocks in the first and the second tower respectively.
The second line contains integers (), where is the height of the -th block of the first tower counting from the bottom; is the bottom block and is the top block.
The third line contains integers (), where is the height of the -th block of the second tower counting from the bottom; is the bottom block and is the top block.
Output
Print a single integer: the minimum number of blocks that must be removed so that both towers have the same height.
Hint
