/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 3ms 2.77 MiB
#2 Accepted 3ms 2.77 MiB
#3 Accepted 3ms 2.77 MiB
#4 Accepted 3ms 2.77 MiB
#5 Accepted 5ms 2.77 MiB
#6 Accepted 4ms 2.77 MiB
#7 Accepted 6ms 2.738 MiB
#8 Accepted 10ms 2.977 MiB
#9 Accepted 10ms 2.715 MiB
#10 Accepted 10ms 2.77 MiB
#11 Accepted 10ms 2.77 MiB
#12 Accepted 395ms 467.996 MiB
#13 Accepted 464ms 467.996 MiB
#14 Accepted 470ms 467.797 MiB
#15 Accepted 493ms 467.918 MiB
#16 Accepted 406ms 467.945 MiB
#17 Accepted 405ms 467.84 MiB
#18 Accepted 380ms 467.891 MiB
#19 Accepted 431ms 467.812 MiB
#20 Accepted 381ms 467.812 MiB
#21 Accepted 378ms 467.801 MiB
#22 Accepted 389ms 467.773 MiB
#23 Accepted 398ms 467.887 MiB
#24 Accepted 425ms 467.777 MiB
#25 Accepted 443ms 467.902 MiB
#26 Accepted 372ms 467.77 MiB
#27 Accepted 535ms 467.98 MiB
#28 Accepted 370ms 467.812 MiB
#29 Accepted 389ms 467.922 MiB
#30 Accepted 449ms 467.879 MiB
#31 Accepted 508ms 467.785 MiB
#32 Accepted 520ms 478.594 MiB
#33 Accepted 475ms 478.586 MiB
#34 Accepted 407ms 467.973 MiB
#35 Accepted 420ms 467.984 MiB
#36 Accepted 381ms 467.965 MiB
#37 Accepted 368ms 467.965 MiB
#38 Accepted 423ms 467.879 MiB
#39 Accepted 363ms 467.984 MiB
#40 Accepted 393ms 467.953 MiB
#41 Accepted 395ms 467.98 MiB
#42 Accepted 651ms 467.969 MiB
#43 Accepted 513ms 467.867 MiB
#44 Accepted 472ms 467.891 MiB
#45 Accepted 531ms 467.91 MiB
#46 Accepted 396ms 467.77 MiB
#47 Accepted 758ms 467.824 MiB
#48 Accepted 498ms 467.848 MiB
#49 Accepted 374ms 467.82 MiB
#50 Accepted 1532ms 468.016 MiB

Code

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

const int MAXN = 1e5;
const int MAXK = 300;
const long long INF = 1e18;

int A[MAXN + 5];
long long dp[MAXN + 5][MAXK + 2], down[MAXN + 5][MAXK + 2];

vector<int> X[MAXN + 5];

void dfs(int u, int par, int k)
{
    down[u][1] = dp[u][1] = A[u];
    
    for (int i = 2; i <= k; i++)
        dp[u][i] = down[u][i] = -INF;
    
    int a = -1, b = -1;
    
    for (int v: X[u])
        if (v != par)
        {
            dfs(v, u, k);
            
            if (a == -1)
                a = v;
            else
                b = v;
            
            for (int i = 1; i <= k; i++)
            {
                dp[u][i] = max(dp[u][i], dp[v][i]);
                down[u][i] = max(down[u][i], down[v][i - 1] + A[u]);
            }
        }
    
    for (int i = 1; i <= k; i++)
    {
        dp[u][i] = max(dp[u][i], down[u][i]);
    
        if (b != -1)
        {
            for (int j = 1; j <= i - j - 1; j++)
            {
                dp[u][i] = max(dp[u][i], down[a][j] + down[b][i - j - 1] + A[u]);
                dp[u][i] = max(dp[u][i], down[b][j] + down[a][i - j - 1] + A[u]);
            }
        }
        
        if (dp[u][i] < 0)
            dp[u][i] = -INF;
    }
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(NULL), cout.tie(NULL);
    
    int n, k;
    cin >> n >> k;
    
    for (int i = 1; i <= n; i++)
        cin >> A[i];
    
    for (int i = 1; i <= n - 1; i++)
    {
        int u, v;
        cin >> u >> v;
        
        X[u].push_back(v);
        X[v].push_back(u);
    }
    
    dfs(1, 0, k);
    long long res = max(0LL, dp[1][k]);
    
    cout << res << "\n";
    
    return 0;
}

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:48:53
Judged At
2025-02-17 16:48:53
Judged By
Score
100
Total Time
1532ms
Peak Memory
478.594 MiB