# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
199675 | algorithm16 | Chase (CEOI17_chase) | C++14 | 4067 ms | 51704 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>
using namespace std;
typedef long long int llint;
vector <int> v[100005];
llint arr[100005],s[100005],dp[100005][105],n,v1;
void dfs(int x,int par) {
for(int j=0;j<v[x].size();j++) {
if(v[x][j]==par) continue;
dfs(v[x][j],x);
}
for(int i=0;i<=v1;i++) {
for(int j=0;j<v[x].size();j++) {
if(v[x][j]==par) continue;
dp[x][i]=max(dp[x][i],dp[v[x][j]][i]);
if(i) dp[x][i]=max(dp[x][i],dp[v[x][j]][i-1]+s[x]-arr[par]);
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> v1;
for(int i=0;i<n;i++) {
cin >> arr[i];
}
for(int i=0;i<n-1;i++) {
int a,b;
cin >> a >> b;
v[a-1].push_back(b-1);
v[b-1].push_back(a-1);
}
for(int i=0;i<n;i++) {
for(int j=0;j<v[i].size();j++) {
s[i]+=arr[v[i][j]];
}
}
for(int i=0;i<=v1;i++) {
dfs(0,-1);
}
cout << dp[0][v1];
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... |