The Bovine Accordion and Banjo Orchestra

Interview

Time limit1sMemory limit128 MB

Summary
Choose increasing pairs between two length-N sequences to maximize the sum of A_i*B_j minus the squared sums of each maximal block of unpaired elements on both sides.
Level

Medium7 of 10

Topics
Dynamic programming, Prefix sum, Math, Implementation
Solved
No attempts yet

Problem

There are 2N2N cows (3≤N≤10003 \le N \le 1000) forming an orchestra: NN accordionists and NN banjoists. Accordionist ii has a talent level AiA_i and banjoist jj has a talent level BjB_j, where 0≤Ai≤10000 \le A_i \le 1000 and 0≤Bj≤10000 \le B_j \le 1000.

Farmer John pairs accordionists with banjoists to hold concerts. Pairing accordionist ii with banjoist jj earns exactly Ai⋅BjA_i \cdot B_j dollars in donations.

The musicians are stubborn about their seating order, so the pairs must preserve the original order: if accordionist ii is paired with banjoist jj, then no accordionist whose index is greater than ii may be paired with a banjoist whose index is less than jj. Equivalently, if the chosen pairs are written in increasing order of accordionist index, their banjoist indices must be strictly increasing as well. As a result, some cows may have to be left unpaired.

Every unpaired cow is upset. Consider the accordionists on their own and break the unpaired ones into maximal groups of consecutive indices. A group whose talents sum to SS costs S2S^2 dollars (spent drowning their sorrows in orange soda). The same rule applies independently to the unpaired banjoists.

Formally, if accordionists xx through yy are all unpaired and form one maximal consecutive group, they cost (Ax+Ax+1+⋯+Ay)2(A_x + A_{x+1} + \cdots + A_y)^2 dollars, and the identical relationship holds for banjoists.

Farmer John foots this bill, so he accounts for it when choosing the pairings. Find the maximum net profit (total donations minus total soda cost) he can achieve.

Input

  • The first line contains a single integer NN.
  • The next NN lines contain A1,A2,…,ANA_1, A_2, \ldots, A_N, one value per line.
  • The following NN lines contain B1,B2,…,BNB_1, B_2, \ldots, B_N, one value per line.

Output

Print a single integer: the maximum net profit Farmer John can earn.

Notes

Worked example: suppose there are three accordionists with talents 1,1,51, 1, 5 and three banjoists with talents 5,1,15, 1, 1. The best plan pairs accordionist 33 (talent 55) with banjoist 11 (talent 55), earning 5×5=255 \times 5 = 25 dollars. The two unpaired accordionists (talents 11 and 11) form one group costing (1+1)2=4(1 + 1)^2 = 4 dollars, and the two unpaired banjoists (talents 11 and 11) form another group costing (1+1)2=4(1 + 1)^2 = 4 dollars. The net profit is 25−4−4=1725 - 4 - 4 = 17.

Because pairing accordionist ii with banjoist ii for every ii is always a valid choice, the answer is never negative. The profits and costs can be large, so 64-bit integers are required.

Examples3

  1. Example 1

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

    Input
    3
    0
    0
    0
    0
    0
    0
    
    Expected output
    0
    
  3. Example 3

    Input
    3
    1000
    1000
    1000
    1000
    1000
    1000
    
    Expected output
    3000000