/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 184ms 239.016 MiB
#2 Accepted 182ms 239.02 MiB
#3 Accepted 185ms 239.062 MiB
#4 Wrong Answer 184ms 239.039 MiB
#5 Accepted 186ms 238.949 MiB
#6 Wrong Answer 188ms 238.855 MiB

Code

#include <bits/stdc++.h>
using namespace std;

#define all(x) (x).begin(), (x).end()
#define repeat(x) for(int taramtam = 0;taramtam<(x);taramtam++)

template<typename S, typename T> void semax(S &a, const T &b) {if (a<b) a = b;}
template<typename S, typename T> void semin(S &a, const T &b) {if (a>b) a = b;}

void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail>
void dbg_out(Head H, Tail... T) { cerr << ' ' << H;dbg_out(T...);}
#define debug(...) cerr << "(" #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)

const int INF = 1e9+100;
const int MOD = 1e9+7;

#define int long long
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template<typename S>
void displayvec(vector<S> a){
    for (S thing:a){
        cout << thing << ' ';
    }
    cout << '\n';
}
template <typename S> 
void readvec(vector<S>& a){
    for (S& thing:a){
        cin>>thing;
    }
}
vector<int> weights(100001);
vector<vector<int>> adj(100001);
vector<vector<int>> adj2(100001);
vector<vector<int>> small(100001, vector<int>(300, -1000000000000000000));
int dp(int n, int k){
    if (small[n][k]>=0)return small[n][k];
    if(k==0)return weights[n];
    small[n][0]=weights[n];
    int m = -1000000000000000000;
    for(auto thing:adj2[n]){
        m=max(m, dp(thing, k-1));
    }
    return m+weights[n];
}
void dfs(int n, int p){
    for(auto thing:adj[n]){
        if (thing!=p){
            adj2[n].push_back(thing);
            dfs(thing, n);
        }
    }
}
void solve(){
    int n, k;cin>>n>>k;
    for(int i = 1;i<=n;i++)cin>>weights[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, -1);
    int res=  0;
    for(int i = 1;i<=n;i++){
        
        res=max(res, dp(i, k-1));
    }
    cout << res << '\n';

}







signed main() {
    ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
    //freopen("compin.txt", "r", stdin);
	//freopen("compout.txt", "w", stdout);
    int TestCase = 1;
    //cin >> TestCase;
    while(TestCase--) solve();
}

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 15:41:33
Judged At
2025-02-17 15:41:33
Judged By
Score
8
Total Time
188ms
Peak Memory
239.062 MiB