이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const int MOD = (int)1e9 + 7;
const int MAXN = (int)1e5 + 7;
const int MAXS = (int)107;
const int infint = (int)1e9;
const ll inf = 1e18;
ll n, v, dp[MAXN][MAXS][2], P[MAXN], val[MAXN];
vector<ll> G[MAXN];
void dfs(int u, int p)
{
if(p == -1)
{
dp[u][0][0] = 0;
dp[u][1][1] = val[u];
}
else
{
for (int j = 0; j <= v; j++)
{
dp[u][j][0] = max(dp[p][j][1], dp[p][j][0]);
if(j)
dp[u][j][1] = max(dp[p][j - 1][0] + val[u] - P[p], dp[p][j - 1][1] + val[u] - P[p]);
}
}
for (auto v : G[u])
if(v != p)
dfs(v, u);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n >> v;
for (int i = 1; i <= n; i++)
cin >> P[i];
for (int i = 0; i < n - 1; i++)
{
ll u, v;
cin >> u >> v;
val[v] += P[u];
val[u] += P[v];
G[u].push_back(v);
G[v].push_back(u);
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= v; j++)
dp[i][j][0] = -inf, dp[i][j][1] = -inf;
ll root = 6;
dfs(root, -1);
ll ans = 0;
for (int i = 1; i <= n; i++)
for (int j = 0; j <= v; j++)
{
ans = max(ans, max(dp[i][j][0], dp[i][j][1]));
}
cout << ans;
}
# | 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... |