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. CAD在图纸保存的同时,也把基本信息保存了(网页版)

    主要用到函数说明: MxDrawXCustomFunction::Mx_SaveDwgToURLEx 保存DWG文件到服务器上的扩展函数.详细说明如下: 参数 说明 pszServerUrl 服务器网 ...

  2. 《LeetCode-0004》 寻找两个有序数组的中位数-Median of Two Sorted Arrays

    题目给定两个大小为 m 和 n 的有序数组nums1和 nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n)). 你可以假设 nums1 和 nums2 ...

  3. iic通讯 FPGA实现 mpu6050为例

    IIC最常用的通讯协议,但普遍用于单片机.arm这些,用FPGA实现大材小用,但对于菜鸡水平练练手很不错,考验串并转换和时序的控制.今天我就以mpu6050陀螺仪为例,实现FPGA的iic通信. 1. ...

  4. SqlServer IsNull 与 NullIf

    ISNULL(check_expression, replacement_value) check_expression 与 replacement_value 数据类型必须一致,如果 check_e ...

  5. tipsText表单验证(注册)

    注册表单验证脚本 <script src="/assets/skins/js/jquery-1.11.2.min.js"></script> <scr ...

  6. vim学习1-入门指令

    使用vim命令进入vim界面 vim后面加上你要打开的已存在的文件名或者不存在(则作为新建文件)的文件名. 打开Xfce终端,输入以下命令 $ vim practice_1.txt 直接使用vim也可 ...

  7. Codeforces 263C. Appleman and Toastman

    C. Appleman and Toastman time limit per test  2 seconds memory limit per test  256 megabytes input  ...

  8. mongoDB全文索引

    相关文章:php使用Coreseek实现全文索引 Introduction Mongo provides some functionality that is useful for text sear ...

  9. 暑假集训D17总结

    考试 玄学的一次考试= = T1乱搞 只会乱搞出前二十分  然后真的拿了二十分 T2模拟 自己造数据 没有一个是在十分钟内跳出来的 然后竟然A了 T3暴力 觉得如果老爷机心情不好就会被卡到20  然后 ...

  10. class类加载机制

    1.类的加载过程 a.加载-链接-初始化-使用-卸载 加载: 查找并加载类的二进制数据 链接: 验证类的正确性,为类的静态变量分配内存,并将其初始化为默认值,把类的符号引用转换为直接引用. 初始化: ...