Two Decks
InterviewTime limit1sMemory limit1024 MB
Wonbin removes at most K items from the top or bottom of two stacks to minimize the weight of the heavier backpack he is left carrying.
- Level
Medium5 of 10
- Topics
- Prefix sum, Brute force, Array
- Solved
- No attempts yet
Problem
Seunghyeong and Wonbin prepared two backpacks for a trip. Each backpack holds items.
- The first backpack has items with weights stacked from bottom to top. is the weight of the item on top.
- The second backpack has items with weights stacked from bottom to top. is the weight of the item on top.
The weight of a backpack is the sum of the weights of the items left inside it. Wonbin can perform at most operations. In each operation, he picks one of the two backpacks and removes the item on the top or the bottom of it. The chosen backpack's weight drops by the weight of the removed item.
After Wonbin finishes, Seunghyeong decides which backpack to carry. Being shrewd, he always carries the lighter of the two. If the two weights are equal, he carries either one. Wonbin must carry the backpack that Seunghyeong does not choose.
Find the minimum possible weight of the backpack Wonbin has to carry.
Input
The first line contains two integers and (, ).
The second line contains integers (), the item weights in the first backpack.
The third line contains integers (), the item weights in the second backpack.
Output
Print the minimum possible weight of the backpack that Wonbin has to carry.