# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
140376 | abacaba | Dostavljač (COCI18_dostavljac) | C++14 | 70 ms | 65540 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 <bits/stdc++.h>
using namespace std;
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define mp make_pair
#define f first
#define se second
#define pb push_back
#define ppb pop_back
#define ll long long
#define ull unsigned long long
#define cntbit(x) __builtin_popcount(x)
#define uset unordered_set
#define umap unordered_map
#define pii pair<int, int>
#define ld long double
#define pll pair<long long, long long>
const int inf = 2e9;
const int N = 5e3 + 15;
int n, m, a[N], dp[N][N], ans;
vector <int> g[N];
void dfs(int v, int p = -1) {
for(int to : g[v]) {
if(to != p) {
for(int k = m - 1; k >= 0; --k) {
dp[to][k + 1] = max(dp[to][k + 1], dp[v][k]);
if(k + 2 <= m)
dp[to][k + 2] = max(dp[to][k + 2], dp[v][k] + a[to]);
}
dfs(to, v);
for(int k = m - 1; k >= 0; --k)
dp[v][k + 1] = max(dp[v][k + 1], dp[to][k]);
}
}
}
int main() {
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; ++i)
scanf("%d", &a[i]);
for(int i = 1; i < n; ++i) {
int u, v;
scanf("%d%d", &u, &v);
g[u].pb(v);
g[v].pb(u);
}
for(int i = 0; i < N; ++i)
fill(dp[i], dp[i] + N, -inf);
dp[1][0] = 0, dp[1][1] = a[1];
dfs(1);
for(int i = 1; i <= n; ++i)
ans = max(ans, *max_element(dp[i], dp[i] + m + 1));
printf("%d\n", ans);
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... |
# | 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... |