PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#CC0000064. Thuê ba bảng hiển thị (C - Three displays)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

2 lộ trình

Thuê ba bảng hiển thị (C - Three displays)

C - Three displays

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn displays arranged in order. Display ii has font size sis_i and rental cost cic_i. Choose exactly three indices i<j<ki<j<k with si<sj<sks_i<s_j<s_k and minimize ci+cj+ckc_i+c_j+c_k. Print -1 if no such triple exists.

Input

The first line contains nn; the second contains nn font sizes sis_i; the third contains nn costs cic_i.

Output

Print the minimum total cost, or -1 if no valid triple exists.

Subtasks

Original constraints: 3≤n≤30003\le n\le3000; 1≤si≤1091\le s_i\le10^9; 1≤ci≤1081\le c_i\le10^8.

  • Subtask 1 (20 points): 3≤n≤123\le n\le12; 1≤si≤1001\le s_i\le100; 1≤ci≤1001\le c_i\le100.
  • Subtask 2 (30 points): 3≤n≤3003\le n\le300; 1≤si≤10000001\le s_i\le1000000; 1≤ci≤10000001\le c_i\le1000000.
  • Subtask 3 (50 points): 3≤n≤30003\le n\le3000; 1≤si≤10000000001\le s_i\le1000000000; 1≤ci≤1000000001\le c_i\le100000000.

Examples

Input

5
2 4 5 4 10
40 30 20 10 40

Output

90

Explanation

Choose positions 1,4,51,4,5: font sizes are 2<4<102<4<10, and the cost is 40+10+40=9040+10+40=90.