/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 182ms 239.785 MiB
#2 Accepted 184ms 239.695 MiB
#3 Accepted 188ms 239.68 MiB
#4 Accepted 189ms 239.652 MiB
#5 Wrong Answer 182ms 239.77 MiB
#6 Accepted 189ms 239.805 MiB
#7 Accepted 190ms 239.664 MiB
#8 Accepted 188ms 239.586 MiB
#9 Wrong Answer 190ms 239.789 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));
            continue;
        }
        for(int j = 0;j<=k;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:34:30
Judged At
2025-02-17 16:34:30
Judged By
Score
14
Total Time
190ms
Peak Memory
239.805 MiB