atcoder#AGC048B. [AGC048B] Bracket Score

[AGC048B] Bracket Score

Score : 700700 points

Problem Statement

In this problem, we consider strings consisting of (, ), [, and ].

A string xx is said to be a good parentheses sequence if and only if one of the following conditions is satisfied:

  • xx is an empty sequence.
  • There is a good parentheses sequence ss such that concatenating (, ss, and ) in this order results in xx.
  • There is a good parentheses sequence ss such that concatenating [, ss, and ] in this order results in xx.
  • There are non-empty good parentheses sequences ss and tt such that concatenating ss and tt in this order results in xx.

For example, [], ([()]), and ()[()] are good parentheses sequences, but neither ()) nor ([)] is a good parentheses sequence.

Given are an even number NN and integer sequences AA and BB of length NN each. Here, we define a score for a good parentheses sequence of length NN, s=s1s2sNs=s_1s_2\cdots s_N, as follows:

  • The score for ss is the sum of the scores of its characters.
  • The score for the ii-th character (1iN1 \leq i \leq N) is AiA_i if sis_i is ( or ), and BiB_i if sis_i is [ or ].

Find the maximum possible score for a good parentheses sequence of length NN.

Constraints

  • 2N1052 \leq N \leq 10^5
  • NN is an even number.
  • 1Ai1091 \leq A_i \leq 10^9
  • 1Bi1091 \leq B_i \leq 10^9
  • All numbers in input are integers.

Input

Input is given from Standard Input in the following format:

NN

A1A_1 A2A_2 \cdots ANA_N

B1B_1 B2B_2 \cdots BNB_N

Output

Print the answer.

4
4 2 3 1
2 3 2 4
12

For s=s=()[], the score is A1+A2+B3+B4=12A_1+A_2+B_3+B_4=12, which is the maximum possible value.

10
866111664 844917655 383133839 353498483 472381277 550309930 378371075 304570952 955719384 705445072
178537096 218662351 231371336 865935868 579910117 62731178 681212831 16537461 267238505 318106937
6629738472