/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 166ms 239.629 MiB
#2 Accepted 164ms 239.77 MiB
#3 Accepted 169ms 239.746 MiB
#4 Accepted 165ms 239.688 MiB
#5 Accepted 163ms 239.816 MiB
#6 Accepted 167ms 239.59 MiB
#7 Accepted 164ms 239.582 MiB
#8 Accepted 163ms 239.633 MiB
#9 Accepted 162ms 239.719 MiB
#10 Accepted 165ms 239.629 MiB
#11 Accepted 160ms 239.648 MiB
#12 Accepted 425ms 245.242 MiB
#13 Accepted 1236ms 245.27 MiB
#14 Accepted 1341ms 245.316 MiB
#15 Accepted 1421ms 245.516 MiB
#16 Accepted 899ms 245.371 MiB
#17 Accepted 735ms 245.41 MiB
#18 Accepted 588ms 245.387 MiB
#19 Accepted 1022ms 245.328 MiB
#20 Accepted 534ms 245.293 MiB
#21 Accepted 479ms 245.277 MiB
#22 Accepted 726ms 245.332 MiB
#23 Accepted 758ms 245.234 MiB
#24 Accepted 1009ms 245.27 MiB
#25 Accepted 1101ms 245.27 MiB
#26 Accepted 288ms 245.379 MiB
#27 Accepted 1687ms 245.27 MiB
#28 Accepted 322ms 245.352 MiB
#29 Accepted 504ms 245.328 MiB
#30 Accepted 1224ms 245.434 MiB
#31 Accepted 1555ms 245.27 MiB
#32 Accepted 1037ms 253.387 MiB
#33 Accepted 753ms 253.477 MiB
#34 Accepted 858ms 245.148 MiB
#35 Accepted 981ms 245.09 MiB
#36 Accepted 586ms 245.176 MiB
#37 Accepted 412ms 244.945 MiB
#38 Accepted 985ms 245.098 MiB
#39 Accepted 360ms 244.949 MiB
#40 Accepted 750ms 245.098 MiB
#41 Accepted 789ms 245.066 MiB
#42 Accepted 2170ms 245.34 MiB
#43 Accepted 1595ms 245.371 MiB
#44 Accepted 1405ms 245.383 MiB
#45 Accepted 1696ms 245.312 MiB
#46 Accepted 806ms 245.27 MiB
#47 Accepted 2443ms 245.23 MiB
#48 Accepted 1486ms 245.398 MiB
#49 Accepted 442ms 245.27 MiB
#50 Time Exceeded ≥3111ms ≥245.328 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;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:35:50
Judged At
2025-02-17 16:35:50
Judged By
Score
98
Total Time
≥3111ms
Peak Memory
≥253.477 MiB