#include <bits/stdc++.h>
#define FOR(i, beg, ed, etr) for(ll i = beg;i <= ed;i += etr)
#define FORN(i, beg, ed, etr) for(ll i = beg;i >= ed;i -= etr)
#define all(x) x.begin(), x.end()
#define F first
#define pb push_back
#define con continue
#define S second
using namespace std;
typedef long long ll;
typedef double db;
typedef long double ld;
typedef pair<int,int> pii;
const ll inf = 1e9;
const ll INF = 1e13;
const ll N = 5e3 + 5;
const ll maxn = 5e6;
const ll MOD = 998244353;
ll a[N];
ll f[N];
vector <int> g[N];
ll dep[N],id[N];
ll dp[N],suf[N];
void solve(){
int n;cin >> n;
FOR(i, 1, n, 1){
cin >> a[i];
dp[i] = INF;
}
FOR(i, 1, n, 1){
cin >> f[i];
}
suf[n] = a[n];
FORN(i, n-1, 1, 1){
suf[i] = min(suf[i+1],f[i]);
}
FOR(i, 2, n, 1){
ll u,v;cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
}
dep[1] = 1;
dp[1] = suf[a[1] + 1];
FOR(i, 2, n, 1){
dp[i] = dp[i-1] + suf[a[i] + 1];
ll mx = a[i];
int was = 0,res = 0;
FORN(j, i - 1, 1, 1){
if(a[j] + 1 == a[j+1]){
if(res != 0 && a[j+1] >= res)break;
}
else if(a[j+1] == 0){
if(was == 0){
if(res == 0)res = a[j] + 1;
if(res <= mx){
break;
}
was = 1;
}
else{
if(res != a[j] + 1)break;
}
}
else break;
mx = max(mx,a[j]);
if(was == 0){
dp[i] = min(dp[i],dp[j-1] + suf[mx+1]);
}
else{
dp[i] = min(dp[i],dp[j-1] + f[res]);
}
}
}
cout << dp[n];
}
main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
ll abd = 1;
// cin >> abd;
FOR(i, 1, abd, 1){
solve();
}
}
컴파일 시 표준 에러 (stderr) 메시지
code1.cpp:78:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
78 | main(){
| ^~~~| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |