# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
780592 | vjudge1 | Dostavljač (COCI18_dostavljac) | C++17 | 144 ms | 2288 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;
vector < int > g[505];
int dp[505][505][2];
int cst[505];
void solve(int p,int u) {
for (int i=0;i<g[u].size();i++) {
int to=g[u][i];
if (to == p) continue;
solve(u,to);
for (int now=500;now>=0;now--)
for (int x=1;x+2+now<=503;x++) {
dp[u][now+2+x][1]=max(dp[u][now+2+x][1],dp[u][now][1]+dp[to][x][1]);
dp[u][now+2+x][0]=max(dp[u][now+2+x][0],dp[u][now][0]+dp[to][x][1]);
dp[u][now+1+x][0]=max(dp[u][now+1+x][0],dp[u][now][1]+dp[to][x][0]);
dp[u][now+1+x][0]=max(dp[u][now+1+x][0],dp[u][now][1]+dp[to][x][1]);
}
}
for (int now=500;now>=1;now--)
dp[u][now][0]=max(dp[u][now][0],dp[u][now-1][0]+cst[u]),
dp[u][now][1]=max(dp[u][now][1],dp[u][now-1][1]+cst[u]);
}
int n,m,i,a,b,res;
int main() {
cin>>n>>m;
for (i=1;i<=n;i++)
cin>>cst[i];
for (i=1;i<n;i++) {
scanf("%d%d",&a,&b);
g[a].push_back(b);
g[b].push_back(a);
}
solve(0,1);
res=0;
for (i=0;i<=m;i++)
res=max(res,max(dp[1][i][0],dp[1][i][1]));
cout<<res<<endl;
}
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... |