Submission #683838

#TimeUsernameProblemLanguageResultExecution timeMemory
683838Theo830Chase (CEOI17_chase)C++17
40 / 100
4058 ms262444 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e9+7;
const ll MOD = 998244353;
typedef pair<ll,ll> ii;
#define iii pair<ll,ii>
#define f(i,a,b) for(int i = a;i < b;i++)
#define pb push_back
#define vll vector<ll>
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
///I hope I will get uprating and don't make mistakes
///I will never stop programming
///sqrt(-1) Love C++
///Please don't hack me
///@TheofanisOrfanou Theo830
///Think different approaches (bs,dp,greedy,graphs,shortest paths,mst)
///Stay Calm
///Look for special cases
///Beware of overflow and array bounds
///Think the problem backwards
///Training
ll p[100005];
ll dp[300005][105];
vector<vector<ii> >adj;
ll solve(ll idx,ll v,ll par,ll d){
    if(dp[d][v] != -1){
        return dp[d][v];
    }
    ll ans = 0;
    ll sum = 0;
    for(auto x:adj[idx]){
        if(x.F != par){
            sum += p[x.F];
            ans = max(ans,solve(x.F,v,idx,x.S));
        }
    }
    if(v){
        for(auto x:adj[idx]){
            if(x.F != par){
                ans = max(ans,sum  + solve(x.F,v-1,idx,x.S));
            }
        }
    }
    return dp[d][v] = ans;
}
int main(void){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    ll n,v;
    cin>>n>>v;
    f(i,0,n){
        cin>>p[i+1];
    }
    adj.assign(n+5,vector<ii>());
    ll cur = n+5;
    f(i,0,n-1){
        ll a,b;
        cin>>a>>b;
        adj[a].pb(ii(b,cur));
        cur++;
        adj[b].pb(ii(a,cur));
        cur++;
    }
    ll ans = 0;
    memset(dp,-1,sizeof dp);
    f(i,1,n+1){
        ans = max(ans,solve(i,v,0,i));
    }
    cout<<ans<<"\n";
}
/*
12 2
2 3 3 8 1 5 6 7 8 3 5 4
2 1
2 7
3 4
4 7
7 6
5 6
6 8
6 9
7 10
10 11
10 12
*/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...