CF思维联系– Codeforces-987C - Three displays ( 动态规划)
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的復雜度竟然能這么快。
總結
以上是生活随笔為你收集整理的CF思维联系– Codeforces-987C - Three displays ( 动态规划)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java亦或(^)
- 下一篇: 奔驰车熄火十分钟后自燃 车主:未进行任何