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 n n$ displays placed along a road, and the $ i i i$-th of them can display a text with font size $ s i s_i si$ only. Maria Stepanovna wants to rent such three displays with indices $ i < j < k i < j < k i<j<k$ that the font size increases if you move along the road in a particular direction. Namely, the condition $ s i < s j < s k s_i < s_j < s_k si<sj<sk$ should be held.
The rent cost is for the $ i i i$-th display is $ c i c_i ci$. Please determine the smallest cost Maria Stepanovna should pay.
Input
The first line contains a single integer $
n
n
n$ ($
3
≤
n
≤
3
000
3 \le n \le 3\,000
3≤n≤3000$) — the number of displays.
The second line contains $ n n n$ integers $ s 1 , s 2 , … , s n s_1, s_2, \ldots, s_n s1,s2,…,sn$ ($ 1 ≤ s i ≤ 1 0 9 1 \le s_i \le 10^9 1≤si≤109$) — the font sizes on the displays in the order they stand along the road.
The third line contains $ n n n$ integers $ c 1 , c 2 , … , c n c_1, c_2, \ldots, c_n c1,c2,…,cn$ ($ 1 ≤ c i ≤ 1 0 8 1 \le c_i \le 10^8 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
i < j < k
i<j<k$ such that $
s
i
<
s
j
<
s
k
s_i < s_j < s_k
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
Note
In the first example you can, for example, choose displays $
1
1
1$, $
4
4
4$ and $
5
5
5$, because $
s
1
<
s
4
<
s
5
s_1 < s_4 < s_5
s1<s4<s5$ ($
2
<
4
<
10
2 < 4 < 10
2<4<10$), and the rent cost is $
40
+
10
+
40
=
90
40 + 10 + 40 = 90
40+10+40=90$.
In the second example you can’t select a valid triple of indices, so the answer is -1.
题意:根据第一组序列找一个递增三元组。这个递增三元组对应着三个下面序列的值。求最小的三元组的和。
思想:我们枚举中间一个值,找i前面小于a[i]的最小的值,并且找i后面大于a[i]的最小的值。不断更新最小值。O(n^2)的时间复杂度
代码如下:
#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int maxx=3e3+100;
int a[maxx],b[maxx];
int n;
int main()
{
set<int> s;
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
for(int i=1;i<=n;i++) scanf("%d",&b[i]);
int _max3=inf;
for(int i=1;i<=n;i++)
{
int _max1=inf,_max2=inf;
for(int j=1;j<i;j++) if(a[j]<a[i]) _max1=min(_max1,b[j]);
for(int j=i+1;j<=n;j++) if(a[j]>a[i]) _max2=min(_max2,b[j]);
_max3=min(_max3,_max1+_max2+b[i]);
}
if(_max3==inf) puts("-1");
else printf("%d\n",_max3);
}
努力加油a啊,(o)/~