CF987C.Three displays

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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 nn displays placed along a road, and the ii -th of them can display a text with font size sis_i only. Maria Stepanovna wants to rent such three displays with indices i<j<ki < j < k that the font size increases if you move along the road in a particular direction. Namely, the condition si<sj<sks_i < s_j < s_k should be held.

The rent cost is for the ii -th display is cic_i . Please determine the smallest cost Maria Stepanovna should pay.

输入格式

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

The second line contains nn integers s1,s2,,sns_1, s_2, \ldots, s_n ( 1si1091 \le s_i \le 10^9 ) — the font sizes on the displays in the order they stand along the road.

The third line contains nn integers c1,c2,,cnc_1, c_2, \ldots, c_n ( 1ci1081 \le c_i \le 10^8 ) — the rent costs for each display.

输出格式

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<j<ki < j < k such that si<sj<sks_i < s_j < s_k .

输入输出样例

  • 输入#1

    5
    2 4 5 4 10
    40 30 20 10 40
    

    输出#1

    90
    
  • 输入#2

    3
    100 101 100
    2 4 5
    

    输出#2

    -1
    
  • 输入#3

    10
    1 2 3 4 5 6 7 8 9 10
    10 13 11 14 15 12 13 13 18 13
    

    输出#3

    33
    

说明/提示

In the first example you can, for example, choose displays 11 , 44 and 55 , because s1<s4<s5s_1 < s_4 < s_5 ( 2<4<102 < 4 < 10 ), and the rent cost is 40+10+40=9040 + 10 + 40 = 90 .

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

首页