Submission #321074

# Submission time Handle Problem Language Result Execution time Memory
321074 2020-11-10T20:57:07 Z forza_viola Dostavljač (COCI18_dostavljac) C++14
140 / 140
243 ms 3392 KB
//#include <GOD>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define len length	
#define FAST_IO ios_base::sync_with_stdio(false); cin.tie(NULL);
#define frei freopen("input.txt","r",stdin)
#define freo freopen("output.txt","w",stdout)

const ll inf=1e18;
const ll mod=1e9+7;
const ll maxn=500+10,maxl=2e6+10;

ll n,m,a[maxn],ans,mark[maxn];
vector <ll> nei[maxn];
ll dp[maxn][maxn][2];

void dfs(ll u){
	mark[u]=1;
	dp[u][0][0]=dp[u][0][1]=0;
	dp[u][1][0]=dp[u][1][1]=a[u];
	for(auto v:nei[u]){
		if(!mark[v]){
			dfs(v);
			for(int i=m;i>=0;i--){
				for(int j=0;j<i;j++){
					if(i>=j+2){
						dp[u][i][0]=max(dp[u][i][0],dp[u][i-j-2][0]+dp[v][j][1]);
						dp[u][i][1]=max(dp[u][i][1],dp[u][i-j-2][1]+dp[v][j][1]);
					}
					dp[u][i][0]=max(dp[u][i][0],dp[u][i-j-1][1]+dp[v][j][0]);
				}
			}
		}
	}
}
int main(){
	ios_base::sync_with_stdio(false),cin.tie(NULL);
	cin>>n>>m;
	for(int i=0;i<n;i++){
		cin>>a[i];
	}
	for(int i=0;i<n-1;i++){
		ll u,v;
		cin>>u>>v;
		nei[u-1].pb(v-1);
		nei[v-1].pb(u-1);
	}
	dfs(0);
	ll ans=0;
	for(int i=0;i<=m;i++){
		ans=max(ans,dp[0][i][0]);
	}
	cout<<ans<<endl;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 876 KB Output is correct
2 Correct 3 ms 876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1004 KB Output is correct
2 Correct 34 ms 1132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 66 ms 1508 KB Output is correct
2 Correct 21 ms 1636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 2028 KB Output is correct
2 Correct 144 ms 1788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 124 ms 2788 KB Output is correct
2 Correct 53 ms 3044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 243 ms 3392 KB Output is correct
2 Correct 23 ms 2540 KB Output is correct