Submission #717286

# Submission time Handle Problem Language Result Execution time Memory
717286 2023-04-01T19:19:28 Z Ahmed57 Dostavljač (COCI18_dostavljac) C++14
14 / 140
275 ms 4284 KB
#include <bits/stdc++.h>

using namespace std;
long long dp[2][501][501];
long long 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[0][j][c]+dp[1][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 Incorrect 1 ms 468 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 724 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 848 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 8 ms 1104 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 1492 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 72 ms 1808 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 26 ms 2644 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 142 ms 3412 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 275 ms 4284 KB Output isn't correct
2 Halted 0 ms 0 KB -