Codeforces Round 485 (Div. 2)
C
Three displays
Codeforces
1400
原题
← BD →
翻译中…

It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in Zabaikalsky region), wants to rent three displays to highlight an important problem.

翻译中…

There are $n$ displays placed along a road, and the $i$-th of them can display a text with font size $s_i$ only. Maria Stepanovna wants to rent such three displays with indices $i \lt j \lt k$ that the font size increases if you move along the road in a particular direction. Namely, the condition $s_i \lt s_j \lt s_k$ should be held.

翻译中…

The rent cost is for the $i$-th display is $c_i$. Please determine the smallest cost Maria Stepanovna should pay.

翻译中…

Input

翻译中…

The first line contains a single integer $n$ ($3 \le n \le 3\,000$) — the number of displays.

翻译中…

The second line contains $n$ integers $s_1, s_2, \ldots, s_n$ ($1 \le s_i \le 10^9$) — the font sizes on the displays in the order they stand along the road.

翻译中…

The third line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 10^8$) — the rent costs for each display.

翻译中…

Output

翻译中…

If there are no three displays that satisfy the criteria, print -1. Otherwise print a single integer — the minimum total rent cost of three displays with indices $i \lt j \lt k$ such that $s_i \lt s_j \lt s_k$.

翻译中…

Note

翻译中…

In the first example you can, for example, choose displays $1$, $4$ and $5$, because $s_1 \lt s_4 \lt s_5$ ($2 \lt 4 \lt 10$), and the rent cost is $40 + 10 + 40 = 90$.

翻译中…

In the second example you can't select a valid triple of indices, so the answer is -1.

翻译中…

样例

Input #1
5
2 4 5 4 10
40 30 20 10 40
Output #1
90
InputCopy OutputCopy
Input #2
3
100 101 100
2 4 5
Output #2
-1
InputCopy OutputCopy
Input #3
10
1 2 3 4 5 6 7 8 9 10
10 13 11 14 15 12 13 13 18 13
Output #3
33
InputCopy OutputCopy
题目信息
题号
987C
时间限制
1 s
内存限制
256 MB
标签
暴力
动态规划
模拟

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。