/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 1ms 464.0 KiB
#3 Accepted 1ms 320.0 KiB
#4 Accepted 1ms 492.0 KiB
#5 Accepted 1ms 320.0 KiB
#6 Accepted 1ms 436.0 KiB
#7 Accepted 1ms 352.0 KiB
#8 Accepted 1ms 524.0 KiB
#9 Accepted 1ms 320.0 KiB
#10 Accepted 1ms 484.0 KiB
#11 Accepted 1ms 540.0 KiB
#12 Accepted 131ms 7.078 MiB
#13 Time Exceeded ≥3096ms ≥7.066 MiB
#14 Time Exceeded ≥3100ms ≥7.02 MiB

Code

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
const ll mod = 1e9 + 7;

ll n , k;
vector<ll> arr;

ll dfs(vector<ll> graph[] , ll source , ll par , ll k){
    if(k == 0) return 0;
    ll ans = arr[source];
    for(auto child : graph[source]){
        if(child == par) continue;
        if(k) ans = max(ans , dfs(graph , child , source , k - 1) + arr[source]);
    }
    
    return ans;
}


int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    
    cin >> n >> k;
    arr.resize(n + 1);
    for(ll i = 1 ; i <= n ; i++){
        cin >> arr[i];
    }
    
    vector<ll> graph[n + 1];
    for(ll i = 1 ; i < n ; i++){
        ll x , y;
        cin >> x >> y;
        graph[x].push_back(y);
        graph[y].push_back(x);
    }
    
    ll ans = 0;
    for(ll i = 1 ; i <= n ; i++){
        ans = max(ans, dfs(graph , i , -1 , k));
    }
    
    cout << ans << "\n";
    
    
    return 0;
}
// Author : Raj (raj_singh35)

Information

Submit By
Type
Submission
Problem
P1160 Max path sum (Easy Version)
Contest
Brain Booster #8
Language
C++17 (G++ 13.2.0)
Submit At
2025-02-17 16:03:19
Judged At
2025-02-17 16:03:19
Judged By
Score
24
Total Time
≥3100ms
Peak Memory
≥7.078 MiB