ACM思维题训练集合

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 n displays placed along a road, and the i-th of them can display a text with font size si only. Maria Stepanovna wants to rent such three displays with indices i<j<k that the font size increases if you move along the road in a particular direction. Namely, the condition si<sj<sk should be held.

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

Input

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

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

The third line contains n integers c1,c2,…,cn (1≤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<k such that si<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

一看到这题,我觉得像是个背包,实际上差不多,只不过就是有了限制条件,后选的序号一定大于之前的序号,且给定的S[i]也需要大于之前选的。然后这个题我觉得数据有点水n2n^2n2的复杂度竟然能这么快。

#include <bits/stdc++.h>
using namespace std;
template <typename t>
void read(t &x)
{
char ch = getchar();
x = 0;
int f = 1;
while (ch < '0' || ch > '9')
f = (ch == '-' ? -1 : f), ch = getchar();
while (ch >= '0' && ch <= '9')
x = x * 10 + ch - '0', ch = getchar();
x *f;
}
#define wi(n) printf("%d ", n)
#define wl(n) printf("%lld ", n)
#define rep(m, n, i) for (int i = m; i < n; ++i)
#define P puts(" ")
typedef long long ll;
#define MOD 1000000007
#define mp(a, b) make_pair(a, b)
//---------------https://lunatic.blog.csdn.net/-------------------//
const int N = 3005;
const int INF = 0x3f3f3f3f;
int s[N], cost[N], maxi[N];
int dp[N][10], weight[N][10];
int main()
{
int n;
read(n);
rep(1, n + 1, i)
{
read(s[i]);
}
rep(1, n + 1, i)
{
read(cost[i]);
}
memset(dp, 0x3f, sizeof(dp));
//rep(0, n + 1, i) weight[i] =s[i];
rep(1, n, i)
{
dp[i][1]=cost[i];
weight[i][1]=s[i];
for (int j =2; j <= 3; j++)
{
for (int k = i+1; k <= n; k++)
if (s[k] > weight[i][j-1])
{
//cout<<1;
if (dp[k][j] > dp[i][j - 1] + cost[k])
{
//cout<<2;
dp[k][j] = dp[i][j - 1] + cost[k];
weight[k][j] = s[k];
}
}
}
}
int ans = INF;
rep(1, n + 1, i)
ans = min(ans, dp[i][3]);
if (ans == INF)
puts("-1");
else
{
wi(ans);
P;
}
}

最新文章

  1. 邮箱输入(仿gmail)
  2. Redis集群方案介绍
  3. Win2008R2PHP5.4环境加载Zend模块
  4. C# 使用AutoResetEvent进行线程同步
  5. mysql复制表数据或表结构到新表中
  6. flask开发restful api系列(8)-再谈项目结构
  7. 如何在Windows Server 2016启用或关闭Internet Explorer增强的安全配置
  8. 用JavaScript实现图片剪切效果
  9. centos基本命令
  10. session,cookie,sessionStorage,localStorage的区别
  11. C# word 图片大小
  12. spring+springMvc+struts的SSH框架整合
  13. 2.5 Visio2007不规则图形填充
  14. [Spark][python]RDD的collect 作用是什么?
  15. IDEA非sbt下spark开发
  16. 20155334 《网络攻防》 Exp 8 Web基础
  17. SQL Server跨服务器查询的实现方法,OpenDataSource
  18. webpack初入门
  19. requirejs配置代码示例
  20. Maven 项目中使用mybatis-generator生成代码

热门文章

  1. Python 0(安装及初步使用+学习资源推荐)
  2. spark 大杂烩
  3. 返回指定字符串位置的函数FIELD(S,S1,S2,...) 与 FIND_IN_SET(S1,S2) 函数
  4. 使用Network Emulator Toolkit工具模拟网络丢包测试(上)
  5. Android Them+SharedPreferences 修改程序所有view字体颜色、大小和页面背景
  6. Davor COCI 2018
  7. (转) 关于Windows CE和Windows Mobile
  8. elasticsearch7.6.2实战(2)-es可视化及分析平台-kibana
  9. Python-selenium-自动化测试模型
  10. D - Complete Tripartite