Submission #98247

#TimeUsernameProblemLanguageResultExecution timeMemory
98247dalgerokDostavljač (COCI18_dostavljac)C++17
140 / 140
266 ms2432 KiB
#include<bits/stdc++.h> using namespace std; const int N = 505; int n, m, a[N], dp[N][N][2]; vector < int > g[N]; void dfs(int v, int pr = -1){ dp[v][0][0] = dp[v][0][1] = 0; dp[v][1][1] = dp[v][1][0] = a[v]; for(int to : g[v]){ if(to != pr){ dfs(to, v); for(int i = m; i >= 1; i--){ for(int j = 0; j < i; j++){ if(i - j - 1 >= 0){ dp[v][i][0] = max(dp[v][i][0], dp[v][j][1] + dp[to][i - j - 1][0]); } if(i - j - 2 >= 0){ dp[v][i][1] = max(dp[v][i][1], dp[v][j][1] + dp[to][i - j - 2][1]); dp[v][i][0] = max(dp[v][i][0], dp[v][j][0] + dp[to][i - j - 2][1]); } } } } } } int main(){ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); cin >> n >> m; memset(dp, -63, sizeof(dp)); for(int i = 1; i <= n; i++){ cin >> a[i]; } for(int i = 1; i < n; i++){ int x, y; cin >> x >> y; g[x].push_back(y); g[y].push_back(x); } dfs(1); int ans = 0; for(int i = 1; i <= m; i++){ ans = max({ans, dp[1][i][0], dp[1][i][1]}); } cout << ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...