记录编号 577212 评测结果 AAAAAAAAAA
题目名称 盗取资料 最终得分 100
用户昵称 Gravatarlihaoze 是否通过 通过
代码语言 C++ 运行时间 0.403 s
提交时间 2022-10-26 10:18:34 内存使用 6.19 MiB
显示代码纯文本
#include "bits/stdc++.h"

const int N = 2e5 + 10;
int n, m;
int a[N], f[N], size[N];
int e[N], ne[N], h[N], idx;

void add(int a, int b) {
    e[idx] = b, ne[idx] = h[a], h[a] = idx ++;
}

void dfs(int u, int d, int last) {
    size[u] = a[u];
    for (int i = h[u]; i != -1; i = ne[i]) {
        int v = e[i];
        if (v != last) {
            dfs(v, d + 1, u);
            size[u] += size[v];
        }
    }
    f[1] += a[u] * d;
}

void dp(int u, int last) {
    for (int i = h[u]; i != -1; i = ne[i]) {
        int v = e[i];
        if (v != last) {
            f[v] = f[u] - size[v] + (size[1] - size[v]);
            dp(v, u);
        }
    }
}

int main() {
    freopen("dqzl.in", "r", stdin); 
    freopen("dqzl.out", "w", stdout);
    memset(h, -1, sizeof h);
    std::cin >> n >> m;
    for (int i = 1; i <= n; ++ i) {
        std::cin >> a[i];
    }
    for (int i = 1, x, y; i < n; ++ i) {
        std::cin >> x >> y;
        add(x, y), add(y, x);
    }
    dfs(1, 0, 0);
    dp(1, 0);
    int ans = 1e9;
    for (int i = 1; i <= n; ++ i) {
        ans = std::min(ans, f[i]);
    }
    if (ans <= m) {
        std::cout << ans << '\n';  
    } else {
        std::cout << "Poor Van" << '\n';
    }
    return 0;
}