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>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <climits>
#include <cmath>
#include <fstream>
#include <queue>
#include <stack>
#include <iomanip>
#include <bitset>
//#include "network.h"
using namespace std;
#define ll long long
//cin.tie(0);ios_base::sync_with_stdio(0);
const int MAXN = 1005;
ll ans = 0;
vector<int> nums;
vector<int> adjList[MAXN];
int n, m;
void dfs(int src, int par, int v, ll tot) {
ans = max(ans, tot);
if (v == 0) {
return;
}
for (auto i : adjList[src]) {
if (i == par) {
continue;
}
tot += nums[i];
}
for (auto i : adjList[src]) {
if (i == par) {
continue;
}
dfs(i, src, v - 1, tot);
}
}
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++) {
ll temp;
cin >> temp;
nums.push_back(temp);
}
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
a--;
b--;
adjList[a].push_back(b);
adjList[b].push_back(a);
}
for (int i = 0; i < n; i++) {
dfs(i, -1, m, 0);
}
cout << ans << endl;
}
# | 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... |