# bzoj 4557 [JLoi2016]侦察守卫

http://www.elijahqi.win/archives/3698
Description

。换句话说，游戏的地图是一棵有N个节点的树。游戏中有一种道具叫做侦查守卫，当一名玩家在一个点上放置侦

Input

500000,D<=20
Output

Sample Input
12 2
8 9 12 6 1 1 5 1 4 8 10 6
10
1 2 3 5 6 7 8 9 10 11
1 3
2 3
3 4
4 5
4 6
4 7
7 8
8 9
9 10
10 11
11 12
Sample Output
10
HINT

Source

x是第0层 现在可以额外覆盖子树以外i层的东西

#include<cstdio>
#include<cctype>
#include<algorithm>
using namespace std;
inline char gc(){
static char now[1<<16],*S,*T;
if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;}
return *S++;
}
inline int read(){
int x=0,f=1;char ch=gc();
while(!isdigit(ch)) {if (ch=='-') f=-1;ch=gc();}
while(isdigit(ch)) x=x*10+ch-'0',ch=gc();
return x*f;
}
const int N=5e5+10;
const int inf=0x3f3f3f3f;
int n,m,w[N],q,h[N],f[N][22],g[N][22],num;
bool mark[N];
struct node{
int y,next;
}data[N<<1];
inline void dfs(int x,int fa){
g[x][0]=f[x][0]=mark[x]?w[x]:0;
for (int i=1;i<=m;++i) g[x][i]=w[x];g[x][m+1]=inf;
for (int owo=h[x];owo;owo=data[owo].next){
int y=data[owo].y;if (y==fa) continue;dfs(y,x);
for (int i=0;i<=m;++i) g[x][i]=min(g[x][i]+f[y][i],f[x][i+1]+g[y][i+1]);
for (int i=m;~i;--i) g[x][i]=min(g[x][i],g[x][i+1]);
f[x][0]=g[x][0];
for (int i=1;i<=m;++i) f[x][i]+=f[y][i-1];
for (int i=1;i<=m;++i) f[x][i]=min(f[x][i],f[x][i-1]);
}
}
int main(){
freopen("bzoj4557.in","r",stdin);
n=read();m=read();
for (int i=1;i<=n;++i) w[i]=read();q=read();
for (int i=1;i<=q;++i) mark[read()]=1;
for (int i=1;i<n;++i){
int x=read(),y=read();
data[++num].y=y;data[num].next=h[x];h[x]=num;
data[++num].y=x;data[num].next=h[y];h[y]=num;
}dfs(1,0);printf("%d\n",f[1][0]);
return 0;
}