I don't really know what the people in this line of the tree are thinking, online incredibly no long chain dissection of the problem, hey, this question is a standard long chain, without merging, deep right, sort take before k big on line, SCOI2016 day1 T1 original, I can't spit slot.
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace
Std
Template<class t>inline void Read (T &res) {static char ch;register int flag=1; while ((Ch=getchar ()) < ' 0 ' | |
Ch> ' 9 ') if (ch== '-') flag=-1;res=ch-48;
while ((Ch=getchar ()) >= ' 0 ' &&ch<= ' 9 ') Res=res*10+ch-48;res*=flag;
const int N = 200005;
int n,k,tot,fa[n],head[n],son[n];
Long Long ans,sz[n],g[n],num[n],dep[n]; struct data{int to,nxt;}
e[n<<1]; inline void Addedge (int x,int y) {e[++tot].nxt=head[x],head[x]=tot,e[tot].to=y;} void dfsf (int x,int f) {fa[x]=f;
SZ[X]=DEP[X];
for (register int v,i=head[x];i;i=e[i].nxt) {if (v=e[i].to,v!=f) {DEP[V]=DEP[X]+NUM[V],DFSF (v,x);
if (Sz[v]>sz[son[x]]) son[x]=v,sz[x]=sz[v];
}} void DFSs (int x,long long sum) {if (son[x]) DFSS (son[x],sum+num[son[x]);
else g[x]=sum; for (register int v,i=head[x];i;i=e[i].nxt) {if (V=e[i]. To,v!=fa[x]&&v!=son[x]) DFSS (V,num[v]);
int CMP (long long A,long long b) {return a>b;} int main () {read (n), read (k);
for (register int i=1;i<=n;i++) read (Num[i]);
for (register int x,y,i=1;i<n;i++) read (x), read (y), Addedge (X,y), Addedge (y,x);
DFSF (1,0), DFSS (1,num[1]);
Sort (g+1,g+1+n,cmp);
for (register int i=1;i<=k;i++) ans+=g[i];
printf ("%lld", ans);
return 0; }