#P15590. [ICPC 2020 Jakarta R] Comic Binge

[ICPC 2020 Jakarta R] Comic Binge

Problem Description

A long vacation is coming! Andi and Budi, who are siblings, have just got a set of comic books as a gift from their uncle. As they had no plans for this vacation, they decided to stay home and read comic books.

There are NN comic books, numbered from 1 to NN. It takes AiA_i hours for Andi to finish reading the ithi^{th} book, while Budi needs BiB_i hours to do so. Both Andi and Budi will read the comic books in ascending order of book number, one book at a time.

Because there is only one set of comic books, they decided to agree on the following rules:

  • Budi, being the older brother, may skip one book after reading a book. More precisely, after Budi has finished reading the ithi^{th} book, he may start reading the (i+2)th(i+2)^{th} book without reading the (i+1)th(i+1)^{th} book at all.
  • If Budi does not skip the ithi^{th} book, Andi, being the younger brother, is not allowed to start reading the ithi^{th} book before Budi finished reading it. If Budi skips the ithi^{th} book, there is no special condition for the book.

Both Andi and Budi must read the 1st1^{st} book and NthN^{th} book to enjoy the storyline. Also, Andi wants to read all books, while Budi might skip some books according to the above rules. Andi and Budi are considered as finished reading the comic books if and only if both Andi and Budi have finished reading the NthN^{th} book.

Your task in this problem is to determine the shortest time in hours for Andi and Budi to finish reading the comic books.

Input Format

Input begins with a line containing an integer: NN (1N10001 \leq N \leq 1000) representing the number of comic books. The second line contains NN integers: AiA_i (1Ai10001 \leq A_i \leq 1000) representing the time in hours needed for Andi to read each book. The third line contains NN integers: BiB_i (1Bi101 \leq B_i \leq 10) representing the time in hours needed for Budi to read each book.

Output Format

Output in a line an integer representing the shortest time in hours for Andi and Budi to finish reading the comic books.

6
3 1 1 1 1 2
1 5 3 3 7 4
13
2
2 1
1 1
4

Hint

Explanation for the sample input/output #1

The shortest time of 1313 hours can be achieved if Budi only read book 11, 33, 44, and 66. One possible reading schedule is as follows.

Hour Andi's reading Budi's reading
1 Book 1
2 Book 1 Book 3
3
4
5 Book 2 Book 4
6 Book 3
7
8 Book 4 Book 6
9
10 Book 5
11
12 Book 6
13

Explanation for the sample input/output #2

Both Andi and Budi have to read book 11 and book N=2N = 2. The shortest time for them to finish all the books is 44 hours. One possible reading schedule is as follows.

Hour Andi's reading Budi's reading
1 Book 1
2 Book 1
3 Book 2
4 Book 2