Under construction

Still being built. Things may change

Brotherly Harmony

Time limit1sMemory limit1024 MB

Summary
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.
Level

Medium6 of 10

Topics
Greedy, Math
Solved
No attempts yet

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 NN paddies. After the harvest, the elder's paddies held a1,a2,⋯ ,aNa_1, a_2, \cdots, a_N bundles, and the younger's paddies held b1,b2,⋯ ,bNb_1, b_2, \cdots, b_N bundles.
  • Their hearts were in tune, so every night they chose the same integers i,ji, j (1≤i<j≤N1 \le i < j \le N). Each brother moved the bundles in their own ii-th paddy to the other's jj-th paddy. Before moving, the ii-th and jj-th paddies of both brothers must still have bundles stacked.
  • They repeated this every night until all bundles were gathered in the NN-th paddy.

Hyeonuk is simulating the ways to gather all bundles into the NN-th paddy. When no more bundles can be moved, what is the maximum possible difference between the amounts of bundles in the two NN-th paddies?

For example, let a1,a2,a3=[2,3,1]a_1, a_2, a_3 = [2, 3, 1] and b1,b2,b3=[1,2,1]b_1, b_2, b_3 = [1, 2, 1]. Here are two ways to gather all bundles in the 3rd paddy.

A way where the amounts in the 3rd paddies differ by 00 when all bundles are gathered there.

A way where the amounts in the 3rd paddies differ by 22 when all bundles are gathered there.

Input

The first line contains an integer NN (2≤N≤200 0002 \le N \le 200\,000).

The second line contains integers a1,a2,⋯ ,aNa_1, a_2, \cdots, a_N (1≤ai≤1 0001 \le a_i \le 1\,000), separated by spaces.

The third line contains integers b1,b2,⋯ ,bNb_1, b_2, \cdots, b_N (1≤bi≤1 0001 \le b_i \le 1\,000), separated by spaces.

Output

Print the maximum possible difference between the amounts of bundles in the two NN-th paddies, when all bundles are gathered in the NN-th paddies in Hyeonuk's simulation.

Examples3

  1. Example 1

    Input
    3
    2 3 1
    1 2 1
    
    Expected output
    2
    
  2. Example 2

    Input
    2
    1 2
    1 3
    
    Expected output
    1
    
  3. Example 3

    Input
    4
    4 3 2 1
    4 3 3 2
    
    Expected output
    0