This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <vector>
using namespace std;
template <typename input_type>
input_type get() {
    input_type input;
    cin >> input;
    return input;
}
const int max_num = (5e2 + 5), max_tmp = 2;
int sz, tm, ppr[max_num], dp[max_tmp][max_num][max_num];
vector <int> nghbr[max_num];
void DFS(int vrtx, int prnt) {
    for (int i = 1; i < (tm + 1); i++) {
        dp[0][vrtx][i] = ppr[vrtx];
        dp[1][vrtx][i] = ppr[vrtx];
    }
    for (int ngh: nghbr[vrtx]) {
        if (ngh != prnt) {
            DFS(ngh, vrtx);
            for (int i = tm; i > 0; i--) {
                for (int j = 0; j < i; j++) {
                    dp[0][vrtx][i] = max(dp[0][vrtx][i], (max(dp[0][ngh][j], dp[1][ngh][j]) + dp[1][vrtx][i - j - 1]));
                    if (j != (i - 1)) {
                        dp[0][vrtx][i] = max(dp[0][vrtx][i], dp[1][ngh][j] + dp[0][vrtx][i - j - 2]);
                        dp[1][vrtx][i] = max(dp[1][vrtx][i], dp[1][ngh][j] + dp[1][vrtx][i - j - 2]);
                    }
                }
            }
        }
    }
}
int main() {
    ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    sz = get<int>(); tm = get<int>();
    for (int i = 1; i < (sz + 1); i++) {
        ppr[i] = get<int>();
    }
    for (int i = 0; i < (sz - 1); i++) {
        pair <int, int> endpnt = {get<int>(), get<int>()};
        nghbr[endpnt.first].push_back(endpnt.second);
        nghbr[endpnt.second].push_back(endpnt.first);
    }
    DFS(1, 0);
    cout << max(dp[0][1][tm], dp[1][1][tm]);
    return 0;
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |