# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
536521 | Asymmetry | Chase (CEOI17_chase) | C++17 | 4064 ms | 95772 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.
//Autor: Bartłomiej Czarkowski
#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
template<class A,class B>auto&operator<<(ostream&o,pair<A,B>p){return o<<'('<<p.first<<", "<<p.second<<')';}
template<class T>auto operator<<(ostream&o,T x)->decltype(x.end(),o){o<<'{';int i=0;for(auto e:x)o<<(", ")+2*!i++<<e;return o<<'}';}
#define debug(x...) cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...);}(x),cerr<<'\n'
#else
#define debug(...) {}
#endif
const int N = 101'000;
const int V = 107;
int n, m, a, b;
int t[N];
int pr[N];
long long sum[N];
vector<int> v[N];
long long dp[N][V];
void dfd(int x) {
for (int i : v[x]) {
if (i == pr[x]) {
continue;
}
pr[i] = x;
dfd(i);
}
}
void dfs(int x) {
for (int i = 0; i <= m; ++i) {
dp[x][i] = 0;
}
for (int i : v[x]) {
if (i == pr[x]) {
continue;
}
dfs(i);
for (int j = 0; j <= m; ++j) {
dp[x][j] = max(dp[x][j], dp[i][j]);
}
}
for (int j = m; j; --j) {
dp[x][j] = max(dp[x][j], dp[x][j - 1] + sum[x] - t[pr[x]]);
}
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i) {
scanf("%d", &t[i]);
}
for (int i = 1; i < n; ++i) {
scanf("%d%d", &a, &b);
v[a].push_back(b);
v[b].push_back(a);
}
for (int i = 1; i <= n; ++i) {
for (int j : v[i]) {
sum[i] += t[j];
}
}
long long odp = 0;
for (int i = 1; i <= n; ++i) {
pr[i] = 0;
dfd(i);
dfs(i);
for (int j = 0; j <= m; ++j) {
odp = max(odp, dp[i][j]);
}
}
printf("%lld\n", odp);
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... |