Problem description

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 si only. Maria Stepanovna wants to rent such three displays with indices i<j<k that the font size increases if you move along the road in a particular direction. Namely, the condition si<sj<sk should be held.

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

Input

The first line contains a single integer n (3≤n≤3000) — the number of displays.

The second line contains n integers s1,s2,…,sn (1≤si≤10^9) — the font sizes on the displays in the order they stand along the road.

The third line contains n integers c1,c2,…,cn (1≤ci≤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<j<k such that si<sj<sk.

Examples

Input

5
2 4 5 4 10
40 30 20 10 40

Output

90

Input

3
100 101 100
2 4 5

Output

-1

Input

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

Output

33

Note

In the first example you can, for example, choose displays 1, 4 and 5, because s1<s4<s5(2<4<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.

解题思路:题目的意思就是找出连续递增的三个数si,sj,sk,使得si<sj<sk,并且使得ci+cj+ck的和最小。做法:从二个数开始循环到倒数第二个数,每循环到当前值sj就向两边遍历查找符合条件的si、sk对应的最小ci,ck值,如果找得到即m1、m2都≠INF,就将三个c值相加再和原来的最小值mincost进行比较替换;最后如果mincost还是INF,说明找不到这样符合条件的情况,此时输出"-1"。时间复杂度为是O(n2),暴力即过。

AC代码:

 #include<bits/stdc++.h>
using namespace std;
const int INF=3e8+;//这里INF要设置成比3e8大一点,因为三个c值相加有可能刚好为3e8
struct NODE{int s,c;}node[];
int main(){
int n,m1,m2,mincost=INF;cin>>n;
for(int i=;i<n;++i)cin>>node[i].s;
for(int i=;i<n;++i)cin>>node[i].c;
for(int i=;i<n-;++i){
m1=m2=INF;
for(int j=i-;j>=;--j)
if(node[j].s<node[i].s&&node[j].c<m1)m1=node[j].c;
for(int j=i+;j<n;++j)
if(node[j].s>node[i].s&&node[j].c<m2)m2=node[j].c;
if(m1!=INF&&m2!=INF)mincost=min(mincost,node[i].c+m1+m2);
}
if(mincost<INF)cout<<mincost<<endl;
else cout<<"-1"<<endl;
return ;
}

U - Three displays的更多相关文章

  1. Codeforces Round #485 (Div. 2) C. Three displays

    Codeforces Round #485 (Div. 2) C. Three displays 题目连接: http://codeforces.com/contest/987/problem/C D ...

  2. CVE-2018-14424 use-after-free of disposed transient displays 分析报告

    漏洞描述 GDM守护进程不能正确的取消导出在D-Bus 接口上已经被销毁的display对象,这造成本地用户可以触发UAF,从而使系统崩溃或造成任意代码执行. 调试环境 gdm版本: 3.14.2(通 ...

  3. Three displays CodeForces - 987C (dp)

    C. Three displays time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  4. DE2之7-segment displays

    以前课题用的是友晶的DE2-70,现在重拾FPGA,选了一款性价比高的DE2.恰逢闲来无事,于是尝试将各个Verilog模块翻译成VHDL,半算回顾以前的知识,半算练习VHDL. Verilog 01 ...

  5. How Chromium Displays Web Pages: Bottom-to-top overview of how WebKit is embedded in Chromium

    How Chromium Displays Web Pages This document describes how web pages are displayed in Chromium from ...

  6. CF 987C Three displays DP或暴力 第十一题

    Three displays time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  7. CF思维联系– Codeforces-987C - Three displays ( 动态规划)

    ACM思维题训练集合 It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in ...

  8. USB Tethering always displays grey when USB tethering type is Linux(EEM)

    USB Tethering always displays grey when USB tethering type is Linux(EEM) 1.Problem DESCRIPTION USB T ...

  9. imshow() displays a white image for a grey image

    Matlab expects images of type double to be in the 0..1 range and images that are uint8 in the 0..255 ...

随机推荐

  1. tomcat 编码设置

    在Tomcat8.0之前的版本,如果你要向服务器提交中文是需要转码的(如果你没有修改server.xml中的默认编码),因为8.0之前Tomcat的默认编码为ISO8859-1. POST方式提交 r ...

  2. C#学习笔记_12_枚举&结构体

    12_枚举&结构体 枚举 是一种数据类型 适用于某些取值范围有限的数据 语法: [访问权限修饰符] enum 枚举名 { 枚举值 } 枚举名遵循大驼峰命名法 枚举一般情况下是和switch c ...

  3. hdu 5652

    India and China Origins Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/ ...

  4. PAT 1103 Integer Factorization

    The K-P factorization of a positive integer N is to write N as the sum of the P-th power of K positi ...

  5. 一个电商项目的Web服务化改造4:方案和架构,通用接口的定义和实现

        最近一直在做一个电商项目,需要把原有单系统架构的项目,改造成基于服务的架构,SOA.     有点挑战,做完了,会有很大进步. 上一篇,我们明确了我们的"规范和约定". 从 ...

  6. 【转】How Many Boyfriends

    如果一个女人遇到不同星座男的概率相同 那么这个女人期望遇到多少个男人就能集齐12个不同星座的男人 我们简化一下问题. 如果只有一个星座,那么期望值为1 如果只有两个星座,那遇到第一个男人后 期望再遇到 ...

  7. 清北学堂模拟赛d2t3 逆序对(pair)

    题目描述LYK最近在研究逆序对.这个问题是这样的.一开始LYK有一个2^n长度的数组ai.LYK有Q次操作,每次操作都有一个参数k.表示每连续2^k长度作为一个小组.假设n=4,k=2,则a[1],a ...

  8. Configuration must specify a spooling directory

    启动spooling源时报错: 原因:spooling配置文件有误 a1.sources.r1.type = spooldir a1.sources.r1.spooldir = /usr/local/ ...

  9. hdu_1039_Easier Done Than Said_201311051511

    Easier Done Than Said? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/O ...

  10. A. Treasure Hunt Codeforces 线性代数

    A. Treasure Hunt time limit per test 1 second memory limit per test 256 megabytes input standard inp ...