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.

题意: 给你n个数,每个数有两个权值(a,b),问取三个数,要求这三个数的a值递增,满足要求的最小三个数的和,没有满足要求的条件输出NO

暴力解法:

遍历中间一个数,然后两个循环分别找比他大的和比他小的,然后记录最小值

dp解法:

dp[i][j],i为第几个选择的数,j是选择的数的位置

这样的话,状态转移方程是:

dp[1][i] 每个位置的数

dp[2][j]这个位置与前面任意位置组合成的递增的两个的数

dp[3][i]这个位置与前面两个位置的组合成递增的三个的数

dp[2][j]=min(dp[2][j],dp[1][i]+dp[1][j]);

dp[3][j]=min(dp[3][j],dp[2][i]+dp[1][j]);

暴力代码:

#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#define debug(a) cout << #a << " " << a << endl
using namespace std;
const int maxn = 1e6 + ;
const int mod = 1e9 + ;
typedef long long ll;
ll a[maxn], b[maxn];
int main(){
std::ios::sync_with_stdio(false);
ll n;
while( cin >> n ) {
for( ll i = ; i < n; i ++ ) {
cin >> a[i];
}
for( ll i = ; i < n; i ++ ) {
cin >> b[i];
}
ll ans = 1e12;
bool flag = false;
for( ll i = ; i < n; i ++ ) {
ll min1 = 1e12, min2 = 1e12;
for( ll j = i+; j < n; j ++ ) {
if( a[i] < a[j] ) {
min1 = min( min1, b[j] );
}
}
for( ll j = ; j < i; j ++ ) {
if( a[i] > a[j] ) {
min2 = min( min2, b[j] );
}
}
if( min1 != 1e12 && min2 != 1e12 ) {
flag = true;
ans = min( ans, min1 + min2 + b[i] );
}
}
if( flag ) {
cout << ans << endl;
} else {
cout << - << endl;
}
}
return ;
}

dp代码:

#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#define debug(a) cout << #a << " " << a << endl
using namespace std;
const int maxn = *1e3 + ;
const int mod = 1e9 + ;
typedef long long ll;
ll a[maxn], b[maxn], dp[maxn][maxn];
int main(){
std::ios::sync_with_stdio(false);
ll n;
while( cin >> n ) {
for( ll i = ; i < n; i ++ ) {
cin >> a[i];
dp[][i] = 1e12, dp[][i] = 1e12;
}
for( ll i = ; i < n; i ++ ) {
cin >> b[i];
}
ll ans = 1e12;
for( ll j = ; j < n; j ++ ) {
for( ll i = ; i < j; i ++ ) {
if( a[i] < a[j] ) {
dp[][j] = min( dp[][j] , b[i] + b[j] );
}
}
}
for( ll j = ; j < n; j ++ ) {
for( ll i = ; i < j; i ++ ) {
if( a[i] < a[j] ) {
dp[][j] = min( dp[][j], dp[][i] + b[j] );
}
}
ans = min( ans, dp[][j] );
}
if( ans != 1e12 ) {
cout << ans << endl;
} else {
cout << - << endl;
}
}
return ;
}

CF 987C Three displays DP或暴力 第十一题的更多相关文章

  1. CF C. Three displays(DP+思维)

    http://codeforces.com/contest/987/problem/C 题意:给你两个n的序列要你根据第一个序列(严格单调递增的方式)在第二个序列里找3个数加起来,输出最小的一个. 思 ...

  2. CCF 201312-4 有趣的数 (数位DP, 状压DP, 组合数学+暴力枚举, 推公式, 矩阵快速幂)

    问题描述 我们把一个数称为有趣的,当且仅当: 1. 它的数字只包含0, 1, 2, 3,且这四个数字都出现过至少一次. 2. 所有的0都出现在所有的1之前,而所有的2都出现在所有的3之前. 3. 最高 ...

  3. 一道看似dp实则暴力的题 Zombie's Treasure Chest

     Zombie's Treasure Chest 本题题意:有一个给定容量的大箱子,此箱子只能装蓝宝石和绿宝石,假设蓝绿宝石的数量无限,给定蓝绿宝石的大小和价值,要求是获得最大的价值 题解:本题看似是 ...

  4. [SHOI2001]化工厂装箱员(dp?暴力:暴力)

    118号工厂是世界唯一秘密提炼锎的化工厂,由于提炼锎的难度非常高,技术不是十分完善,所以工厂生产的锎成品可能会有3种不同的纯度,A:100%,B:1%,C:0.01%,为了出售方便,必须把不同纯度 ...

  5. codeforces 797 E. Array Queries【dp,暴力】

    题目链接:codeforces 797 E. Array Queries   题意:给你一个长度为n的数组a,和q个询问,每次询问为(p,k),相应的把p转换为p+a[p]+k,直到p > n为 ...

  6. VIJOS1476 旅行规划(树形Dp + DFS暴力乱搞)

    题意: 给出一个树,树上每一条边的边权为 1,求树上所有最长链的点集并. 细节: 可能存在多条最长链!最长链!最长链!重要的事情说三遍 分析: 方法round 1:暴力乱搞Q A Q,边权为正-> ...

  7. [cf 1015f] Bracket Substring (dp+kmp)

    传送门 Solution 设dp方程dp[now][pos][red][fla]表示还有now个位置,pos表示匹配到第几位,red表示左括号数-右括号数,fla表示是否已经是给定串的字串 暴力转移即 ...

  8. CF 256D. Good Sequences(DP)

    题目链接 主要是标记前面素数的最大的DP值,要认真一些.没想到居然写了一个很难发现的错误. #include <cstdio> #include <cstring> #incl ...

  9. CF 335B - Palindrome 区间DP

    335B - Palindrome 题目: 给出一个字符串(均有小写字母组成),如果有长度为100的回文子串,输出该子串.否则输出最长的回文子串. 分析: 虽然输入串的长度比较长,但是如果存在单个字母 ...

随机推荐

  1. poj 1455 Crazy tea party

    这道题第一眼看去很难,其实不然,短短几行代码就搞定了. 说一下大概思路,如果是排成一排的n个人,如 1 2 3 4 5 6 7 8 我们要变成 8 7 6 5 4 3 2 1 需要交换 28次,找规律 ...

  2. CentOS7使用yum安装ceph rpm包

    1. 安装centos7对扩展repo的支持yum install yum-plugin-priorities保证下面的选项是开启的[main]enabled = 1 2. 安装 release.ke ...

  3. Netty服务端启动过程相关源码分析

    1.Netty 是怎么创建服务端Channel的呢? 我们在使用ServerBootstrap.bind(端口)方法时,最终调用其父类AbstractBootstrap中的doBind方法,相关源码如 ...

  4. Usaco Training [2.1] The Castle 搜索

    传送门 题目的输出的4个信息 前两个很容易,dfs,bfs都可以,图怎么建都可以 后两个在搜索的时候记录belong[i][j]和已有的size即可 代码应该比不少题解清晰吧 #include < ...

  5. struts的上传下载

    文件上传 添加jar包 commons-io-1.3.2.jar commons-fileupload-1.2.1.jar 前台页面 form表单 method值为post 添加"encty ...

  6. python数据类型图解

  7. 浅谈IDEA集成SSM框架(SpringMVC+Spring+MyBatis)

    前言 学习完MyBatis,Spring,SpringMVC之后,我们需要做的就是将这三者联系起来,Spring实现业务对象管理,Spring MVC负责请求的转发和视图管理, MyBatis作为数据 ...

  8. Android lifecycle 使用详解

    版权声明:本文为博主原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/gdutxiaoxu/article/det ...

  9. npm命令无响应

    npm命令完全无反应,不是加载的那种状态 而是下标不停地在哪里闪... 之后找解决方案,说要删除npmrc文件. 强调:不是nodejs安装目录npm模块下的那个npmrc文件 而是在C:\Users ...

  10. 原生js实现分页功能

    原生就是实现分页功能 代码如下: var pagination = function(option,fun){ this.parentId = option.id; //容器 this.pageSiz ...