/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 186ms 239.727 MiB
#2 Accepted 189ms 239.648 MiB
#3 Accepted 189ms 239.77 MiB
#4 Accepted 185ms 239.602 MiB
#5 Accepted 183ms 239.781 MiB
#6 Accepted 188ms 239.773 MiB
#7 Accepted 188ms 239.703 MiB
#8 Accepted 187ms 239.781 MiB
#9 Accepted 186ms 239.754 MiB
#10 Accepted 186ms 239.621 MiB
#11 Accepted 191ms 239.648 MiB
#12 Accepted 345ms 245.363 MiB
#13 Accepted 809ms 245.426 MiB
#14 Accepted 877ms 245.371 MiB
#15 Accepted 938ms 245.309 MiB
#16 Accepted 623ms 245.246 MiB
#17 Accepted 509ms 245.402 MiB
#18 Accepted 416ms 245.395 MiB
#19 Accepted 682ms 245.27 MiB
#20 Accepted 391ms 245.27 MiB
#21 Accepted 354ms 245.34 MiB
#22 Accepted 498ms 245.387 MiB
#23 Accepted 542ms 245.418 MiB
#24 Accepted 668ms 245.383 MiB
#25 Accepted 720ms 245.293 MiB
#26 Accepted 260ms 245.242 MiB
#27 Accepted 1085ms 245.27 MiB
#28 Accepted 277ms 245.371 MiB
#29 Accepted 398ms 245.328 MiB
#30 Accepted 765ms 245.266 MiB
#31 Accepted 957ms 245.312 MiB
#32 Accepted 750ms 253.457 MiB
#33 Accepted 510ms 253.445 MiB
#34 Accepted 555ms 245.035 MiB
#35 Accepted 642ms 245.07 MiB
#36 Accepted 425ms 245.102 MiB
#37 Accepted 328ms 245.02 MiB
#38 Accepted 654ms 245.062 MiB
#39 Accepted 322ms 245.047 MiB
#40 Accepted 517ms 245.113 MiB
#41 Accepted 595ms 245.02 MiB
#42 Accepted 1354ms 245.273 MiB
#43 Accepted 1008ms 245.324 MiB
#44 Accepted 903ms 245.34 MiB
#45 Accepted 1100ms 245.27 MiB
#46 Accepted 536ms 245.398 MiB
#47 Accepted 1539ms 245.27 MiB
#48 Accepted 965ms 245.336 MiB
#49 Accepted 340ms 245.211 MiB
#50 Accepted 2742ms 245.27 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>(301, -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
P1160 Max path sum (Easy Version)
Contest
Brain Booster #8
Language
C++17 (G++ 13.2.0)
Submit At
2025-02-17 16:39:15
Judged At
2025-02-17 16:39:15
Judged By
Score
100
Total Time
2742ms
Peak Memory
253.457 MiB