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. android------基础面试题

    1. Android的四大组件是哪些,它们的作用? 答:Activity:Activity是Android程序与用户交互的窗口,是Android构造块中最基本的一种,它需要为保持各界面的状态,做很多持 ...

  2. redis 持久化方式

    对于persistence持久化存储,Redis提供了两种持久化方法: Redis DataBase(简称RDB) 执行机制:快照,直接将databases中的key-value的二进制形式存储在了r ...

  3. php的符号的排序大小

  4. MVC实战之排球计分(二)—— 构架概要设计

    本程序主要基于MVC4框架,使应用程序的输入,处理和输出强制性分开,使得软件可维护性,可扩展性,灵活性以及封装性得到提高, MVC应用程序分为三个核心部件:Model,View, Controller ...

  5. kohana操作数据库

    一.读取数据库记录 读取数据库记录需要使用到 DB::select() 方法 // 返回一个结果对象 $result = DB::select('column')->from('table_na ...

  6. oracle数据库中字符乱码

    1.1         88.152 os已安装中文包,以下确认os层面中文是否可以显示 1.2         88.153 os没有安装中文包,以下确认os层面中文无法显示 1.3         ...

  7. Leetcode 860. 柠檬水找零

    860. 柠檬水找零  显示英文描述 我的提交返回竞赛   用户通过次数187 用户尝试次数211 通过次数195 提交次数437 题目难度Easy 在柠檬水摊上,每一杯柠檬水的售价为 5 美元. 顾 ...

  8. WDA基础十三:常用模板管理

    常用的模板一般是SMW0和OAOR,根据不同需求来的. WAD有个不好的地方就是不支持GUI上的OLE和DOI,所以需要做转换,下面是常用的方式: FUNCTION ZCRM_DOWNLOAD_TEM ...

  9. Oracle控制文件

    一.Oracle 控制文件 为二进制文件,初始化大小由CREATE DATABASE指定,可以使用RMAN备份 记录了当前数据库的结构信息,同时也包含数据文件及日志文件的信息以及相关的状态,归档信息等 ...

  10. python安装MySQLdb(即mysql-Python)模块的一些问题

    1.超时问题 如果出现:ReadTimeoutError: HTTPSConnectionPool(host='pypi.python.org', port=443): Read timed out ...