#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <cassert>
using namespace std;
typedef long long ll;
#define ss second
#define ff first
#define all(X) X.begin(), X.end()
#define rall(X) X.rbegin(), X.rend()
#define cinall(X) for(auto &i:X)cin >> i
#define printall(X) for(auto &i:X)cout << i
#define printFromTo(cont, i, j, ch) for(int _ = i; _ <= j; _++)cout << cont[_] << ch
#define readFromTo(cont, i, j) for(int _ = i; _ <= j; _++)cin >> cont[_]
#define fillFromTo(cont, i, j, x) for(int _ = i; _ <= j; _++)cont[_] = x;
#define pb push_back
#define MAKE_UNIQUE_KEEP_ORDER(vec) do { \
unordered_set<decltype((vec).front())> seen; \
(vec).erase(remove_if((vec).begin(), (vec).end(), [&](auto &val) { \
if (seen.count(val)) return true; \
seen.insert(val); \
return false; \
}), (vec).end()); \
} while(0)
#define UNIQUE_SORT(vec) do { \
sort((vec).begin(), (vec).end()); \
(vec).erase(unique((vec).begin(), (vec).end()), (vec).end()); \
} while(0)
#define yes cout << "YES" << endl
#define no cout << "NO" << endl
#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))
#define LB(c, x) (lower_bound((c).begin(), (c).end(), (x)) - (c).begin())
#define UB(c, x) (upper_bound((c).begin(), (c).end(), (x)) - (c).begin())
const int N = 500 + 5;
const int LOG = 30;
const long long INFLL = 1e18;
const int INF = 1e9;
const long double epsilon = 0.000001;
const long long mod = 998244353;
constexpr ll TEN[] = {
1LL,
10LL,
100LL,
1000LL,
10000LL,
100000LL,
1000000LL,
10000000LL,
100000000LL,
1000000000LL,
10000000000LL,
100000000000LL,
1000000000000LL,
10000000000000LL,
100000000000000LL,
1000000000000000LL,
10000000000000000LL,
100000000000000000LL,
1000000000000000000LL,
};
long long binPowByMod(long long x, long long power, long long modx) {
long long res = 1;
long long base = x % modx;
while (power > 0) {
if (power & 1) res = (res * base) % modx;
base = (base * base) % modx;
power >>= 1;
}
return res;
}
void set_IO(string str = "")
{
if (!str.empty()) {
freopen((str + ".in").c_str(), "r", stdin);
freopen((str + ".out").c_str(), "w", stdout);
}
}
int dp[N][N][2], n_dp[N][N][2];
int a[N];
int n, m;
vector<int>G[N];
void dfs(int node, int parent)
{
dp[node][0][0] = dp[node][0][1] = 0;
dp[node][1][1] = dp[node][1][0] = a[node];
for (auto i : G[node])
{
if (i == parent)
continue;
dfs(i, node);
for (int k = 1; k <= m; k++)
{
for (int j = 0; j + k + 2 <= m && j <= k - 2; j++)
{
n_dp[node][k][1] = max(n_dp[node][k][1], dp[i][j][1] + dp[node][k - j - 2][1]);
}
for (int j = 0; j <= k - 1; j++)
{
n_dp[node][k][0] = max(n_dp[node][k][0], dp[node][j][1] + dp[i][k - j - 1][0]);
}
for (int j = 0; j <= k - 2; j++)
{
n_dp[node][k][0] = max(n_dp[node][k][0], dp[node][j][0] + dp[i][k - j - 2][1]);
}
}
for (int k = 0; k <= m; k++)
{
dp[node][k][0] = max(n_dp[node][k][0], dp[node][k][0]);
dp[node][k][1] = max(n_dp[node][k][1], dp[node][k][1]);
}
}
}
void solve()
{
cin >> n >> m;
for (int i = 1; i <= n; i++)cin >> a[i];
for (int i = 1; i < n; i++)
{
int a, b;
cin >> a >> b;
G[a].push_back(b);
G[b].push_back(a);
}
dfs(1, 0);
cout << max(dp[1][m][0], dp[1][m][1]);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
//cin >> t;
while (t--) {
solve();
cout << endl;
}
return 0;
}
Compilation message (stderr)
dostavljac.cpp: In function 'void set_IO(std::string)':
dostavljac.cpp:92:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
92 | freopen((str + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
dostavljac.cpp:93:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
93 | freopen((str + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~| # | 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... |