# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
945082 | vjudge1 | Chase (CEOI17_chase) | C++17 | 4019 ms | 91344 KiB |
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 <cassert>
#include <cstring>
#warning That's the baby, that's not my baby
typedef long long ll;
const int NMAX = 1e5;
const int VMAX = 100;
ll dp[NMAX + 1][VMAX + 1];
int a[NMAX + 1];
int V;
std::vector<int> g[NMAX + 1];
void dfs(int u, int p = 0) {
ll s = 0;
for (const auto &v : g[u]) {
if (v != p) {
dfs(v, u);
s += a[v];
}
}
for (const auto &v : g[u]) {
if (v != p) {
for (int i = 0; i <= V; i++) {
if (i != 0) {
dp[u][i] = std::max(dp[u][i], s + std::max(0LL, dp[v][i - 1]));
}
dp[u][i] = std::max(dp[u][i], dp[v][i]);
}
}
}
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(0);
int n;
std::cin >> n >> V;
for (int i = 1; i <= n; i++) {
std::cin >> a[i];
}
for (int i = 1; i < n; i++) {
int u, v;
std::cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
ll answer = 0;
for (int i = 1; i <= n; i++) {
for (int u = 1; u <= n; u++) {
for (int v = 0; v <= V; v++) {
dp[u][v] = -1e18;
}
}
dfs(i);
answer = std::max(answer, dp[i][V]);
}
std::cout << answer;
return 0;
}
Compilation message (stderr)
# | 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... |