/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 66ms 86.738 MiB
#2 Accepted 67ms 87.02 MiB
#3 Accepted 67ms 87.02 MiB
#4 Accepted 67ms 87.055 MiB
#5 Accepted 66ms 86.836 MiB
#6 Accepted 67ms 86.973 MiB
#7 Accepted 66ms 86.77 MiB
#8 Accepted 66ms 86.777 MiB
#9 Accepted 66ms 86.852 MiB
#10 Accepted 66ms 86.77 MiB
#11 Accepted 67ms 86.895 MiB
#12 Accepted 942ms 92.27 MiB
#13 Accepted 1580ms 92.32 MiB
#14 Accepted 986ms 92.188 MiB
#15 Accepted 1032ms 92.418 MiB
#16 Accepted 528ms 92.324 MiB
#17 Accepted 885ms 92.258 MiB
#18 Accepted 848ms 92.18 MiB
#19 Accepted 1331ms 92.434 MiB
#20 Accepted 532ms 92.27 MiB
#21 Accepted 1591ms 92.316 MiB
#22 Accepted 245ms 92.215 MiB
#23 Accepted 1165ms 92.203 MiB
#24 Accepted 774ms 92.27 MiB
#25 Accepted 434ms 92.27 MiB
#26 Accepted 199ms 92.402 MiB
#27 Accepted 409ms 92.27 MiB
#28 Accepted 183ms 92.414 MiB
#29 Accepted 1393ms 92.359 MiB
#30 Accepted 651ms 92.312 MiB
#31 Accepted 1232ms 92.348 MiB
#32 Accepted 1274ms 92.34 MiB
#33 Accepted 1176ms 92.574 MiB
#34 Accepted 252ms 92.348 MiB
#35 Accepted 457ms 92.352 MiB
#36 Accepted 308ms 92.352 MiB
#37 Accepted 1171ms 92.328 MiB
#38 Accepted 487ms 92.32 MiB
#39 Accepted 1137ms 92.223 MiB
#40 Accepted 611ms 92.277 MiB
#41 Accepted 1322ms 92.395 MiB
#42 Accepted 281ms 92.41 MiB
#43 Accepted 319ms 92.262 MiB
#44 Accepted 194ms 92.699 MiB
#45 Accepted 204ms 92.684 MiB
#46 Accepted 200ms 92.699 MiB
#47 Accepted 304ms 100.609 MiB
#48 Accepted 315ms 96.812 MiB
#49 Accepted 635ms 95.871 MiB
#50 Accepted 180ms 96.02 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;
    }
}
const int nI = -1000000000000000000;
vector<int> weights(100001);
vector<vector<int>> adj(100001);
vector<vector<int>> adj2(100001);
vector<int> parents(100001);
vector<vector<int>> dpt(100001, vector<int>(101, -1));
int dp(int n, int k){
    if (k==-1)return 0;
    if (dpt[n][k]!=-1)return dpt[n][k];
    dpt[n][0]=weights[n];
    if (k==0)return weights[n];
    int m = nI;
    for (auto child:adj2[n]){
        m = max(m, dp(child, k-1));
    }
    dpt[n][k]=m+weights[n];
    return dpt[n][k];
}
//tree is rooted
//for each ones, find the heaviest downward path of all lengths
void dfs(int n, int p){
    for(auto thing:adj[n]){
        if (thing!=p){
            adj2[n].push_back(thing);
            parents[thing]=n;
            dfs(thing, n);
        }
    }
}
void solve(){
    int n, k;cin>>n>>k;
    parents[1]=-1;
    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);
    for(int i = 1;i<=n;i++){
        for(int j = 0;j<k;j++){
            dp(i, j);
        }
    }
    if (k==1){
        cout << *(max_element(all(weights))) << '\n';
        return;
    }
    k--;
    int res = 0;
    for(int i = 1;i<=n;i++){
        //debug(i, res);
        if (adj2[i].size()==0)continue;
        if (adj2[i].size()==1){
            res=max(res, weights[i]+dp(adj2[i][0], k-1));
            continue;
        }
        for(int j = 0;j<=k/2;j++){
            pii pair = {j, k-j};
            vector<pii> w1;
            vector<pii> w2;
            for(auto thing:adj2[i]){
                w1.push_back({dp(thing, pair.first-1), thing});
                w2.push_back({dp(thing, pair.second-1), thing});
            }
            sort(all(w1));
            sort(all(w2));
            reverse(all(w1));
            reverse(all(w2));
            //cout<<'\n';
            //cout << pair.first <<' '<< pair.second <<' '<< i << '\n';
            //for(auto tt:w1){
            //    cout << tt.first << ' ';
            //}
            //cout<<'\n';
            //for(auto tt:w2){
            //    cout << tt.first << ' ';
            //}
            //cout<<'\n';
            if (w1[0].second!=w2[0].second){
                res=max(res, weights[i]+w1[0].first+w2[0].first);
                
            }
            else{
                res=max(res, weights[i]+max(w1[0].first+w2[1].first, w1[1].first+w2[0].first));
            }
            
        }
        //debug(i, res);
    }
    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
P1161 Max path sum (Hard Version)
Contest
Brain Booster #8
Language
C++17 (G++ 13.2.0)
Submit At
2025-02-17 16:40:26
Judged At
2025-02-17 16:40:26
Judged By
Score
100
Total Time
1591ms
Peak Memory
100.609 MiB