Brotherly Harmony
Time limit1sMemory limit1024 MB
Two brothers swap and merge bundle pairs from paddy i into paddy j (i<j) until all bundles sit in paddy N. Find the maximum absolute difference between the two N-th paddies.
Problem
Long ago, in a village, an elder brother and a younger brother got along very well. Each planted rice in the paddy inherited from their late parents, tended it carefully, and harvested it. They stacked the leftover rice bundles in their own paddies. Worried about each other, they secretly moved the bundles from their own paddies to the other's paddy every night. Their hearts were in tune, so the amount of bundles gathered in each other's paddies never changed. One day, when they learned the truth, they hugged each other and wept.
Hyeonuk simulates the story of Brotherly Harmony as follows.
- The brothers inherited paddies from their parents, and the elder and the younger each got paddies. After the harvest, the elder's paddies held bundles, and the younger's paddies held bundles.
- Their hearts were in tune, so every night they chose the same integers (). Each brother moved the bundles in their own -th paddy to the other's -th paddy. Before moving, the -th and -th paddies of both brothers must still have bundles stacked.
- They repeated this every night until all bundles were gathered in the -th paddy.
Hyeonuk is simulating the ways to gather all bundles into the -th paddy. When no more bundles can be moved, what is the maximum possible difference between the amounts of bundles in the two -th paddies?
For example, let and . Here are two ways to gather all bundles in the 3rd paddy.
A way where the amounts in the 3rd paddies differ by when all bundles are gathered there.
A way where the amounts in the 3rd paddies differ by when all bundles are gathered there.
Input
The first line contains an integer ().
The second line contains integers (), separated by spaces.
The third line contains integers (), separated by spaces.
Output
Print the maximum possible difference between the amounts of bundles in the two -th paddies, when all bundles are gathered in the -th paddies in Hyeonuk's simulation.




