题干:

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

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 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.

题目大意:

解题报告:

三元组问题。

类似最长上升子序列的做法,但是维护的不是长度,而是的最小值。

AC代码:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int MAX = 3000 + 5;
const ll INF = 0x3f3f3f3f3f3f3f3f;
ll a[MAX];
ll b[MAX];
ll dp[MAX];
int main()
{int n;ll ans = INF;cin>>n;for(int i = 1; i<=n; i++) {scanf("%lld",&a[i]);}for(int i = 1; i<=n; i++) {scanf("%lld",&b[i]);}memset(dp,INF, sizeof(dp));for(int i =2; i<=n-1; i++) {//其实不用到n,因为还要留一个给第三个display,所以这里递归到n-1.for(int j = 1; j<i; j++) {if(a[i] > a[j])dp[i] = min(dp[i], b[i] + b[j]);}}
//    printf("%lld\n",dp[3]);for(int i = 1; i<=n-1; i++) {for(int j = i+1; j<=n; j++) {if(a[j] > a[i])ans = min(ans ,dp[i]+b[j]);
//            printf("%lld %lld ==== %d %d\n",dp[i],b[j] ,i,j);}}if(ans == INF) printf("-1\n");else cout << ans<<endl;return 0 ;
}

或者直接dp解:

详见博客https://blog.csdn.net/xs18952904/article/details/80504296

20191003

忽然发现今天随便刷codeforce的C题又刷到这道题了,特此更新:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define FF first
#define SS second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;
ll dp1[MAX],dp2[MAX],a[MAX],c[MAX];
int n;
int main()
{cin>>n;for(int i = 1; i<=n; i++) scanf("%lld",a+i),dp1[i]=1e18;for(int i = 1; i<=n; i++) scanf("%lld",c+i),dp2[i]=1e18;for(int i = 1; i<=n; i++) {for(int j = 1; j<i; j++) {if(a[i] > a[j]) dp1[i] = min(dp1[i],c[j]+c[i]);}}for(int i = 1; i<=n; i++) {for(int j = 1; j<i; j++) {if(a[i] > a[j]) dp2[i] = min(dp2[i],dp1[j]+c[i]);}}ll ans = *min_element(dp2+1,dp2+n+1);if(ans == (ll)1e18) ans = -1;printf("%lld\n",ans);return 0 ;
}

【CodeForces - 987C 】Three displays (dp,最长上升子序列类问题,三元组问题)相关推荐

  1. HDU1159(dp最长公共子序列)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Problem Description A subsequ ...

  2. 【HDU - 1025】Constructing Roads In JGShining's Kingdom(dp最长上升子序列模型 + 二分优化)

    题干: Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit ...

  3. 【HDU - 1257】最少拦截系统 (标解dp,贪心可过,最长上升子序列类问题)

    题干: 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能超过前一发的高度.某天,雷达捕捉到敌国的导弹来 ...

  4. UVa 10066 Twin Towers (DP 最长公共子序列)

    题意  求两串数字最长公共子序列的长度 裸的lcs没啥说的 #include<cstdio> #include<cstring> #include<algorithm&g ...

  5. Codeforces 987C. Three displays(o(n^2))

    刚开始三重循环tle test11.后来想了个双重循环的方法. 解题思路: 1.双重循环一次,用一个一位数组存j和比j小的i的和的最小值. 2.再双重循环一次,找到比j大的数k,更新结果为ans=mi ...

  6. 经典dp最长递增子序列

    经典dp最大递增子序列,  看了好长时间,看了好多版本.最终因为这个看懂,也觉得这个是最全面的吧,我感觉我好菜啊. http://wenku.baidu.com/view/bed07b15552707 ...

  7. 【CodeForces - 340D】Bubble Sort Graph (思维,nlogn最长上升子序列类问题)

    题干: Iahub recently has learned Bubble Sort, an algorithm that is used to sort a permutation with n e ...

  8. 【POJ - 1836】Alignment(dp,LIS,最长上升子序列类问题)

    题干: In the army, a platoon is composed by n soldiers. During the morning inspection, the soldiers ar ...

  9. 怪盗基德的滑翔翼 线性DP 最长上升子序列

最新文章

  1. Eclipse 如何安装,以及jdk如何设定环境变量
  2. 刻骨铭心的startActivityForResult三级跳获得第三个Activity中返回的数据
  3. 数学之美 系列十 有限状态机和地址识别
  4. 互联网1分钟 | 0920
  5. 【Boost】boost库中thread多线程详解8——call_once仅运行一次
  6. 1052 卖个萌 (20 分)
  7. PostgreSQL 简单的查询
  8. PyTorch官方教程《Deep Learning with PyTorch》开源分享,LeCun力荐,通俗易懂
  9. 《废柴》系列 - What?废柴,你不会下载Google浏览器插件,Are you kidding???
  10. 创业一定要做自己“喜欢”并且有“优势”的事情
  11. 螺杆泵matlab,基于Matlab与VC++混合编程的螺杆泵转子型线设计
  12. signal(SIGPIPE,SIG_IGN)与signal(SIGCHLD,SIG_IGN)
  13. 海军装备、舰船电子设备|环境可靠性试验|GJB150A\GJB4
  14. 继承ActionSupport 实现Action与属性驱动传参
  15. python遍历文件夹循环处理图片
  16. 第三方支付平台——业务介绍(龙果)
  17. ionic 中的折线图与柱状图
  18. 计算机无法打开这个应用,Windows10打开软件时提示“无法打开这个应用”怎么解决?...
  19. GPS 双频/单频接收机介绍
  20. scratch编程巡线小虫

热门文章

  1. [Leedcode][JAVA][第990题][等式方程的可满足性][并查集]
  2. html5 应用框架,基于HTML5移动应用框架的研究及应用
  3. 定时器取数据时实时进来的数据_Redis-数据淘汰策略amp;持久化方式(RDB/AOF)amp;Redis与Memcached区别...
  4. anaconda中的python如何进行关联分析_Anaconda、Miniconda、Conda、pip的相互关系
  5. 算法入门经典习题第一章
  6. js学习大法:用好firebug,走遍天下都不怕
  7. c语言字符串未初始化strcat,C语言中字符串常用函数strcat与strcpy的用法介绍
  8. notes邮件正文显示不全_python实现一次性批量发邮件
  9. esmini接入外部ego车控制
  10. asterisk 扩展应用(3)——IVR 实现