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. RocketMQ中NameServer的启动

    在RocketMQ中,使用NamesrvStartup作为启动类 主函数作为其启动的入口: public static void main(String[] args) { main0(args); ...

  2. ajax具体实现学习记录

    记录自己对ajax\的理解, 首先要明白ajax是为了解决什么问题,简单来讲就是为了局部刷新页面,而不刷新整个界面.就比如现在有一个实时热度的显示,它是不断变化的,所以你肯定要不停的从数据库当中获取热 ...

  3. 夯实Java基础(七)——Static关键字

    1.static介绍 static关键字一直是各大企业中面试常常会问到的问题,主要考察面试者的基础是否扎实,下面来介绍一下static关键字. Java中static表示“全局”或者“静态”的意思,可 ...

  4. .net开源生态,WTM与NCC

    天下大势,分久必合,合久必分.改朝换代都如花开花谢,过眼云烟,更别提开发语言的更迭了. 我们所坚持的,只是那最初的感动,那“只是在人群中多看了你一眼”的惊艳.三十年河东,三十年河西,不忘初心,方得始终 ...

  5. Java实现调用Bartender控制条码打印机

    官方提供的主要是C#支持. 基于java调用bartender二次开发官方给了一份1998年的J#代码,,,完全用不了,,,百度谷歌搜索万能的网友的答案,发现也没有可参考的.. 最后想到了之前用到了一 ...

  6. egg 自学入门demo分享

    目录 安装 项目 连接数据库 编写model 编写controller 添加路由 2018-08,本文适用于对egg有兴趣想要了解的同学 完整项目代码:https://github.com/NameH ...

  7. CODING 告诉你如何建立一个 Scrum 团队

    原文地址:https://www.atlassian.com/agile/scrum/roles 翻译君:CODING 敏杰小王子 Scrum 当中有三个角色:PO(product owner),敏捷 ...

  8. 面试java后端面经_1

    1 自我介绍(建议提前准备:没准备的可以这样说:来自某学校 姓名 专业 学的啥 为啥学 自己陆陆续续开发的项目 毕业将近 找工作 在哪看到贵公司的招聘 准备了啥 大概这样) 例子:您好!我是来自XXX ...

  9. (三十五)c#Winform自定义控件-Tab页

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  10. vue-cli报错:Class constructor FileManager cannot be invoked without 'new'

    bug:vue-cli3开发的项目,今天项目重新下载依赖启动项目的时候出现错误:Class constructor FileManager cannot be invoked without 'new ...