Link
首先想一个很简单的 O ( n 2 ) O(n^2) O(n2)的 d p dp dp
如果 a i a_i ai这个数字在序列 b b b中出现了,假设 a i a_i ai在 b i b_i bi中是第 g i g_i gi个数字,则设 f i f_i fi为前 i i i个数字去掉某些数字之后通过题录所示的规则恰好能凑出 b b b的前 g i g_i gi个数字的最小代价
然后 f i f_i fi能够从那些满足 a j = b g i − 1 a_j=b_{g_i-1} aj=bgi−1的数字转移过来,假设从 a j a_j aj转移过来,那么 ( j , i ) (j,i) (j,i)这段区间中大于 a j a_j aj的数字必须去掉,不大于 a j a_j aj的数字去不去掉都行,如果权值小于 0 0 0我们就给他去掉(这样收益更高)
我们需要在所有符合条件的 j j j中代价最小的方案
然后发现一个性质,不管 j j j在哪里,只要 k ∈ ( j , i ) k \in (j,i) k∈(j,i)中的一个数字 a k a_k ak大于 a j a_j aj或者 p k < 0 p_k<0 pk<0,那么在 f j f_j fj转移到 f i f_i fi的时候势必会加上 p k p_k pk,所以我可以直接给这个区间的 f f f值加上 p k p_k pk
而这个过程就可以使用线段树维护
#include
#include
#include
#define iinf 0x3f3f3f3f
#define linf (1ll<<60)
#define eps 1e-8
#define maxn 500010
#define cl(x) memset(x,0,sizeof(x))
#define rep(i,a,b) for(i=a;i<=b;i++)
#define drep(i,a,b) for(i=a;i>=b;i--)
#define em(x) emplace(x)
#define emb(x) emplace_back(x)
#define emf(x) emplace_front(x)
#define fi first
#define se second
#define de(x) cerr<<#x<<" = "<
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll read(ll x=0)
{
ll c, f(1);
for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-f;
for(;isdigit(c);c=getchar())x=x*10+c-0x30;
return f*x;
}
struct SegmentTree
{
ll mn[maxn<<2], mx[maxn<<2], sum[maxn<<2], add[maxn<<2], set[maxn<<2], L[maxn<<2], R[maxn<<2];
void maketag_set(ll o, ll v)
{
add[o]=0;
set[o]=v;
mx[o]=mn[o]=v;
sum[o]=(R[o]-L[o]+1)*v;
}
void maketag_add(ll o, ll v)
{
add[o]+=v;
mx[o]+=v, mn[o]+=v;
sum[o]+=(R[o]-L[o]+1)*v;
}
void pushdown(ll o)
{
if(L[o]==R[o])return;
if(~set[o])
{
maketag_set(o<<1,set[o]);
maketag_set(o<<1|1,set[o]);
set[o]=-1;
}
if(add[o])
{
maketag_add(o<<1,add[o]);
maketag_add(o<<1|1,add[o]);
add[o]=0;
}
}
void pushup(ll o)
{
mx[o]=max(mx[o<<1],mx[o<<1|1]);
mn[o]=min(mn[o<<1],mn[o<<1|1]);
sum[o]=sum[o<<1]+sum[o<<1|1];
}
void build(ll o, ll l, ll r, ll* array=NULL)
{
ll mid(l+r>>1);
L[o]=l, R[o]=r;
add[o]=0;
set[o]=-1;
if(l==r)
{
if(array)mn[o]=mx[o]=sum[o]=array[l];
else mn[o]=mx[o]=sum[o]=0;
return;
}
build(o<<1,l,mid,array);
build(o<<1|1,mid+1,r,array);
pushup(o);
}
void Set(ll o, ll l, ll r, ll v)
{
ll mid(L[o]+R[o]>>1);
if(l<=L[o] and r>=R[o]){maketag_set(o,v);return;}
pushdown(o);
if(l<=mid)Set(o<<1,l,r,v);
if(r>mid)Set(o<<1|1,l,r,v);
pushup(o);
}
void Add(ll o, ll l, ll r, ll v)
{
ll mid(L[o]+R[o]>>1);
if(l<=L[o] and r>=R[o]){maketag_add(o,v);return;}
pushdown(o);
if(l<=mid)Add(o<<1,l,r,v);
if(r>mid)Add(o<<1|1,l,r,v);
pushup(o);
}
ll Sum(ll o, ll l, ll r)
{
pushdown(o);
ll mid(L[o]+R[o]>>1), ans(0);
if(l<=L[o] and r>=R[o])return sum[o];
if(l<=mid)ans+=Sum(o<<1,l,r);
if(r>mid)ans+=Sum(o<<1|1,l,r);
return ans;
}
ll Min(ll o, ll l, ll r)
{
ll mid(L[o]+R[o]>>1), ans(linf);
if(l<=L[o] and r>=R[o])return mn[o];
pushdown(o);
if(l<=mid)ans=min(ans,Min(o<<1,l,r));
if(r>mid)ans=min(ans,Min(o<<1|1,l,r));
return ans;
}
ll Max(ll o, ll l, ll r)
{
ll mid(L[o]+R[o]>>1), ans(-linf);
if(l<=L[o] and r>=R[o])return mx[o];
pushdown(o);
if(l<=mid)ans=max(ans,Max(o<<1,l,r));
if(r>mid)ans=max(ans,Max(o<<1|1,l,r));
return ans;
}
}segtree;
ll f[maxn], n, a[maxn], p[maxn], tb[maxn], m, b[maxn], g[maxn], vis[maxn];
int main()
{
ll i;
n=read();
rep(i,1,n)a[i]=read();
rep(i,1,n)p[i]=read();
rep(i,1,n)f[i]=linf;
m=read();
rep(i,1,m)b[i]=read(), tb[b[i]]=i;
rep(i,1,n)g[i]=tb[a[i]];
segtree.build(1,0,n);
segtree.Set(1,1,n,linf);
vis[0]=1;
rep(i,1,n)
{
segtree.Add(1,0,a[i]-1,p[i]);
if(p[i]<0)segtree.Add(1,a[i],n,p[i]);
if(g[i])
{
ll q = segtree.Min(1,b[g[i]-1],b[g[i]-1]) - p[i];
if(vis[g[i]-1] and q<segtree.Min(1,a[i],a[i]))
{
segtree.Set(1,a[i],a[i],q);
vis[g[i]]=1;
// printf("i=%lld q=%lld g[i]=%lld\n",i,q,g[i]);
}
}
}
if(segtree.Min(1,b[m],b[m])>(linf>>1))
{
printf("NO");
}
else
{
printf("YES\n%lld",segtree.Min(1,b[m],b[m]));
}
return 0;
}