이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int ll
#define FOR(i,a,b) for (int i = (a); i<(b); i++)
#define REP(i,n) FOR(i,0,n)
#define REP1(i,n) FOR(i,1,n+1)
#define RREP(i,n) for (int i=(n)-1; i>=0; i--)
#define RREP1(i,n) for (int i=(n); i>=1; i--)
#define f first
#define s second
#define pb push_back
#define ALL(x) x.begin(),x.end()
#define SZ(x) (int)(x.size())
#define SQ(x) (x)*(x)
#define pii pair<int, int>
#define pip pair<int, pii>
#define pdd pair<double ,double>
#define pcc pair<char, char>
#define endl '\n'
//#define TOAD
#ifdef TOAD
#define bug(x) cerr<<__LINE__<<": "<<#x<<" is "<<x<<endl
#define IOS()
#else
#define bug(...)
#define IOS() ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#endif
const double PI=acos(-1);
const ll maxn = 3e5+5;
const int inf = 1<<30;
int n;
vector<int> a(maxn), f(maxn), sf(maxn);
vector<int> aa(maxn), dp(maxn);
vector<int> g[maxn];
signed main(){
//IOS();
cin>>n;
REP1(i, n) cin>>a[i];
REP(i, n) cin>>f[i];
REP(i, n-1){
int u, v; cin>>u>>v;
g[u].pb(v); g[v].pb(u);
}
sf[n] = inf;
RREP(i, n){
sf[i] = min(f[i], sf[i+1]);
}
int cur = 1;
REP1(i, n){
aa[i] = cur;
for (auto v:g[cur]){
if (v != aa[i-1]){
cur = v;
break;
}
}
}
vector<int> va;
va.pb(0);
REP1(i, n){
int A = a[aa[i]];
va.pb(A);
int md = -1;
dp[i] = dp[i-1] + sf[A];
for (int j = i-1; j >= 1; j--){ // if i take j~i
if (va[j+1] == 0){
if (md == -1){
if (va[j] < va[i]) break;
md = va[j];
}
else {
if (va[j] != md) break;
}
dp[i] = min(dp[i], dp[j-1] + f[md]);
}
else {
if (va[j] != va[j+1]-1) break;
if (md == -1) dp[i] = min(dp[i], dp[j-1]+sf[va[i]]);
else dp[i] = min(dp[i], dp[j-1]+f[md]);
}
}
}
cout<<dp[n]<<endl;
}
/*
7
3 1 0 1 2 0 1
8 2 8 6 8 5 8
1 6
7 5
3 4
6 4
2 5
2 3
2 min thonk
19 min write
*/
# | 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... |