제출 #860148

#제출 시각아이디문제언어결과실행 시간메모리
860148ThegeekKnight16Chase (CEOI17_chase)C++17
70 / 100
167 ms96340 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MAXN = 1e5 + 10;
const int MAXV = 110;
array<vector<int>, MAXN> dp;
array<vector<int>, MAXN> grafo;
array<int, MAXN> pom, pomviz;
int N, V;

void dfs(int v, int p)
{
    dp[v].clear(); dp[v].resize(V+1);
    pomviz[v] = 0;
    for (auto viz : grafo[v])
    {
        if (viz == p) continue;
        pomviz[v] += pom[viz];
    }

    for (auto viz : grafo[v])
    {
        if (viz == p) continue;
        dfs(viz, v);
        dp[v][0] = max(dp[viz][0], dp[v][0]);
        for (int k = 1; k <= V; k++)
        {
            dp[v][k] = max({dp[v][k], dp[viz][k], dp[viz][k-1] + pomviz[v]});
        }
    }
}

int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> N >> V;
    for (int i = 1; i <= N; i++) cin >> pom[i];
    for (int i = 1; i < N; i++)
    {
        int X, Y;
        cin >> X >> Y;
        grafo[X].push_back(Y);
        grafo[Y].push_back(X);
    }
    dfs(1, 1);
    int resp = dp[1][V];
    if (N <= 1000)
    {
        for (int i = 2; i <= N; i++)
        {
            dfs(i, i);
            resp = max(resp, dp[i][V]);
        }
    }
    cout << resp << '\n';
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...