-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy path3_segment.cpp
44 lines (38 loc) · 953 Bytes
/
3_segment.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include<bits/stdc++.h>
using namespace std;
// question link
// https://codeforces.com/problemset/problem/987/C
int main() {
#define int long long
int n;
cin>>n;
vector<int>s(n + 1), rent(n + 1);
for(int i = 1 ; i <= n ;i++) {
cin>>s[i];
}
for(int i = 1 ; i <= n ;i ++) {
cin>>rent[i];
}
vector<vector<int>>dp(n+1, vector<int>(4, INT_MAX));
for(int i = 1 ; i <= n; i++) {
dp[i][1] = rent[i];
}
for(int k = 2 ; k <= 3 ;k++) {
for(int i = 1 ; i <= n; i ++) {
for(int j = 1 ; j < i ; j++) {
if(s[j] < s[i]) {
dp[i][k] = min(dp[i][k], dp[j][k-1] + rent[i]);
}
}
}
}
int ans = INT_MAX;
for(int i = 1 ; i <= n ;i++) {
if(dp[i][3] != INT_MAX) {
ans = min(ans, dp[i][3]);
}
}
if(ans!=INT_MAX)
cout<<ans<<"\n";
else cout<<-1<<"\n";
}