C. Three displays
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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 sisi 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<sksi<sj<sk should be held.

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

Input

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

The second line contains nn integers s1,s2,…,sns1,s2,…,sn (1≤si≤1091≤si≤109) — the font sizes on the displays in the order they stand along the road.

The third line contains nn integers c1,c2,…,cnc1,c2,…,cn (1≤ci≤1081≤ci≤108) — 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<ki<j<k such that si<sj<sksi<sj<sk.

Examples
input

Copy
5
2 4 5 4 10
40 30 20 10 40
output

Copy
90
input

Copy
3
100 101 100
2 4 5
output

Copy
-1
input

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

Copy
33
Note

In the first example you can, for example, choose displays 11, 44 and 55, because s1<s4<s5s1<s4<s5 (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.

题意:选三个数,要求:i<j<k 且a[i]<a[j]<a[k],要求选出来的三个数的权值最小

思路:开始总想的是贪心,二分啥啥啥的。。。结果仔细想了下,他的范围是3000, O(n^3)的时间复杂度肯定不行,O(n^2)就可以过

只要我预处理第三个数,在每个数这从后面找一个权值最小且大于它的数,以此来作为第三个数即可,后面只要枚举两个数即可

#include<cstdio>
#include<cmath>
#include<iostream>
#define ll long long
using namespace std;
ll a[],b[],dp[];
int main() {
int n;
scanf("%d",&n);
for(int i=;i<n;i++)
scanf("%d",&a[i]);
for(int i=;i<n;i++)
scanf("%d",&b[i]);
for(int i=;i<n;i++)
{
ll mn=;
for(int j=i+;j<n;j++) {
if(a[i]<a[j]) {
mn=min(mn,b[j]);
}
}
dp[i]=mn;
}
ll ans=;
for(int i=;i<n;i++)
{
for(int j=i+;j<n;j++)
{
if(a[i]<a[j])
{
if(dp[j]!=)
ans=min(ans,b[i]+b[j]+dp[j]);
}
}
}
if(ans==) printf("-1\n");
else cout<<ans<<endl;
}

总结:总的来说这次cf div2的题目不是很难,只是自己刷提还是刷的太少了,没想到思路就卡到了,训练太少,刷题太慢,需要好好反省

规律题总结,看到那种遍历一遍就超时那就肯定是规律题,一 班自己先把公式推出来,然后写个小枚举,把答案输出出来,然后自己再把递推公式推出来,再求解即可

Codeforces Round #485 (Div. 2) C题求三元组(思维)的更多相关文章

  1. Codeforces Round #485 (Div. 2)

    Codeforces Round #485 (Div. 2) https://codeforces.com/contest/987 A #include<bits/stdc++.h> us ...

  2. Codeforces Round #485 (Div. 2) F. AND Graph

    Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...

  3. Codeforces Round #612 (Div. 2) 前四题题解

    这场比赛的出题人挺有意思,全部magic成了青色. 还有题目中的图片特别有趣. 晚上没打,开virtual contest打的,就会前三道,我太菜了. 最后看着题解补了第四道. 比赛传送门 A. An ...

  4. Codeforces Round #378 (Div. 2) D题(data structure)解题报告

    题目地址 先简单的总结一下这次CF,前两道题非常的水,可是第一题又是因为自己想的不够周到而被Hack了一次(或许也应该感谢这个hack我的人,使我没有最后在赛后测试中WA).做到C题时看到题目情况非常 ...

  5. Codeforces Round #485 (Div. 2) D. Fair

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

  6. Codeforces Round #485 (Div. 2) E. Petr and Permutations

    Codeforces Round #485 (Div. 2) E. Petr and Permutations 题目连接: http://codeforces.com/contest/987/prob ...

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

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

  8. Codeforces Round #485 (Div. 2) A. Infinity Gauntlet

    Codeforces Round #485 (Div. 2) A. Infinity Gauntlet 题目连接: http://codeforces.com/contest/987/problem/ ...

  9. Codeforces Round #713 (Div. 3)AB题

    Codeforces Round #713 (Div. 3) Editorial 记录一下自己写的前二题本人比较菜 A. Spy Detected! You are given an array a ...

随机推荐

  1. 启动mysql5.7异常The server quit without updating PID file [FAILED]sql/data/***.pi根本解决方案

    异常表现 mysql5.7启动时报错 Starting MySQL...The server quit without updating PID file [FAILED]sql/data/insta ...

  2. scrapy 爬虫框架之持久化存储

    scrapy  持久化存储 一.主要过程: 以爬取校花网为例 : http://www.xiaohuar.com/hua/ 1.  spider    回调函数     返回item 时    要用y ...

  3. mysql常用修改创建语句

    一.连接数据库 <?php $dbhost = 'localhost:3306'; // mysql服务器主机地址 $dbuser = 'root'; // mysql用户名 $dbpass = ...

  4. python记录_day09 初识函数

    一.认识函数 函数:对动作或者功能的封装 格式: 函数声明     def  函数名(): 函数体 函数调用     函数名() #定义函数 def xiao(): print("你的笑像一 ...

  5. 堆排序 GPLT L2-012 关于堆的判断

    题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805064676261888 分析:这题看起来非常唬人,其实不难 ...

  6. 『MXNet』第十弹_物体检测SSD

    全流程地址 一.辅助API介绍 mxnet.image.ImageDetIter 图像检测迭代器, from mxnet import image from mxnet import nd data_ ...

  7. React文档(七)处理事件

    React元素处理事件和DOM元素处理事件很类似.下面是一些语法的不同之处: React事件的命名是用驼峰命名,而不是小写字母. 利用JSX你传递一个函数作为事件处理器,而不是一个字符串. 举个例子, ...

  8. Binary Analysis Tool安装使用教程

    Binary Analysis Tool(BAT)是一个用于检测二进制文件使用到的开源组件,协助及早发现程序发布后可能会面临的开源协议解执的开源免费检测工具. 一.安装BAT和bat-extratoo ...

  9. VLC添加水印

    Name: LibVLC control APIDescription: VLC media player external control libraryVersion: 2.1.3 参照:http ...

  10. input text 在苹果微信浏览器中有上上阴影

    box-shadow:0px 0px 0px rgba(0,0,0,0); -webkit-appearance:none;