/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 3ms 2.777 MiB
#2 Accepted 2ms 3.027 MiB
#3 Accepted 3ms 2.777 MiB
#4 Accepted 2ms 3.027 MiB
#5 Accepted 3ms 2.777 MiB
#6 Accepted 2ms 3.039 MiB
#7 Accepted 3ms 2.777 MiB
#8 Accepted 2ms 3.027 MiB
#9 Accepted 2ms 2.777 MiB
#10 Accepted 3ms 3.051 MiB
#11 Accepted 3ms 3.074 MiB
#12 Accepted 122ms 6.371 MiB
#13 Time Exceeded ≥3094ms ≥6.223 MiB
#14 Time Exceeded ≥3099ms ≥6.316 MiB

Code

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

// #define int        long long int
#define pb         push_back
#define all(x)     x.begin(),x.end()
#define allr(x)    x.rbegin(),x.rend()
#define ii         pair<long long, long long>
#define endl       '\n'

const int N = 1e5 + 5;
vector<int> g[N];
int a[N], k;

ii dfs(int u, int p, int m) {
    if(m == k)
        return {true, a[u]};
    long long ans = 0, c = 0;
    for(auto &v : g[u]) {
        if(v == p)
            continue;
        ii x = dfs(v, u, m + 1);
        if(x.first) {
            ans = max(ans, x.second);
            c = 1;
        }
    }
    return {c, ans + a[u]};
}

void pipra(int tc) {
    int n;
    cin >> n >> k;
    for(int i = 1 ; i <= n ; i++)
        cin >> a[i];
    for(int i = 1 ; i < n ; i++) {
        int u, v;
        cin >> u >> v;
        g[u].pb(v);
        g[v].pb(u);
    }
    long long ans = 0;
    for(int i = 1 ; i <= n ; i++) {
        ii x = dfs(i, 0, 1);
        if(x.first)
            ans = max(ans, x.second);
    }
    cout << ans << endl;
}

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);

    int t = 1;
    // cin >> t;
    for(int i = 1 ; i <= t ; i++)
        pipra(i);
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1160 Max path sum (Easy Version)
Language
C++17 (G++ 13.2.0)
Submit At
2025-04-11 21:22:30
Judged At
2025-04-11 21:22:30
Judged By
Score
24
Total Time
≥3099ms
Peak Memory
≥6.371 MiB