Submission #717296

# Submission time Handle Problem Language Result Execution time Memory
717296 2023-04-01T19:32:13 Z Ahmed57 Dostavljač (COCI18_dostavljac) C++17
140 / 140
279 ms 4252 KB
#include <bits/stdc++.h>
 
using namespace std;
long long dp[2][501][501];
int n,m;
vector<int> adj[501];
int arr[501];
void dfs(int i,int pr){
    for(int j = 1;j<=m;j++)dp[0][i][j] = arr[i],dp[1][i][j] = arr[i];
    for(auto j:adj[i]){
        if(j==pr)continue;
        dfs(j,i);
        for(int k = m;k>=1;k--){
            for(int c = 1;c<k;c++){
                dp[0][i][k] = max(dp[0][i][k],dp[0][j][c]+dp[1][i][k-(c+1)]);
                if(k>c+1)dp[0][i][k] = max(dp[0][i][k],dp[1][j][c]+dp[0][i][k-(c+2)]);
                if(k>c+1)dp[1][i][k] = max(dp[1][i][k],dp[1][j][c]+dp[1][i][k-(c+2)]);
            }
        }
    }
}
int main(){
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    cin>>n>>m;
    for(int i = 1;i<=n;i++)cin>>arr[i];
    for(int i = 0;i<n-1;i++){
        int a,b;
        cin>>a>>b;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    dfs(1,0);
    cout<<max(dp[0][1][m],dp[1][1][m])<<endl;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 1108 KB Output is correct
2 Correct 3 ms 1108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1492 KB Output is correct
2 Correct 41 ms 1460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 80 ms 1860 KB Output is correct
2 Correct 23 ms 1876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 2648 KB Output is correct
2 Correct 162 ms 2636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 155 ms 3472 KB Output is correct
2 Correct 60 ms 3420 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 279 ms 4252 KB Output is correct
2 Correct 26 ms 4196 KB Output is correct