/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 3ms 5.027 MiB
#2 Accepted 4ms 8.352 MiB
#3 Accepted 4ms 6.203 MiB
#4 Accepted 4ms 8.773 MiB
#5 Accepted 3ms 5.293 MiB
#6 Accepted 3ms 5.527 MiB
#7 Accepted 4ms 6.5 MiB
#8 Accepted 4ms 6.25 MiB
#9 Accepted 3ms 5.777 MiB
#10 Accepted 3ms 5.578 MiB
#11 Accepted 3ms 5.527 MiB
#12 Accepted 549ms 162.551 MiB
#13 Accepted 930ms 162.266 MiB
#14 Accepted 548ms 162.172 MiB
#15 Accepted 613ms 162.316 MiB
#16 Accepted 266ms 162.383 MiB
#17 Accepted 507ms 162.301 MiB
#18 Accepted 484ms 162.355 MiB
#19 Accepted 792ms 162.414 MiB
#20 Accepted 268ms 162.273 MiB
#21 Accepted 929ms 162.27 MiB
#22 Accepted 143ms 162.27 MiB
#23 Accepted 671ms 162.285 MiB
#24 Accepted 448ms 162.344 MiB
#25 Accepted 346ms 162.367 MiB
#26 Accepted 204ms 162.352 MiB
#27 Accepted 345ms 162.371 MiB
#28 Accepted 193ms 162.105 MiB
#29 Accepted 1103ms 162.438 MiB
#30 Accepted 387ms 162.387 MiB
#31 Accepted 707ms 162.316 MiB
#32 Accepted 790ms 162.293 MiB
#33 Accepted 890ms 162.434 MiB
#34 Accepted 152ms 162.152 MiB
#35 Accepted 251ms 162.328 MiB
#36 Accepted 194ms 162.324 MiB
#37 Accepted 740ms 162.422 MiB
#38 Accepted 214ms 162.359 MiB
#39 Accepted 528ms 162.27 MiB
#40 Accepted 275ms 162.211 MiB
#41 Accepted 815ms 162.434 MiB
#42 Accepted 170ms 162.02 MiB
#43 Accepted 190ms 162.191 MiB
#44 Accepted 115ms 162.684 MiB
#45 Accepted 113ms 162.504 MiB
#46 Accepted 113ms 162.578 MiB
#47 Accepted 1230ms 489.43 MiB
#48 Accepted 1173ms 325.867 MiB
#49 Accepted 1121ms 478.707 MiB
#50 Accepted 189ms 203.406 MiB

Code

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

const int MAXN = 1e5;
const int MAXK = 100;
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;
    
    vector< priority_queue<long long, vector<long long>, greater<> > > q(k + 1);
    
    for (int v: X[u])
        if (v != par)
        {
            dfs(v, u, k);
            
            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]);
                
                q[i].push(down[v][i]);
                
                if (q[i].size() > 2)
                    q[i].pop();
            }
        }
    
    vector<long long> maxx(k + 1), max2(k + 1);
    
    for (int i = 1; i <= k; i++)
    {
        if (q[i].empty())
            maxx[i] = max2[i] = -INF;
        else if (q[i].size() == 1)
            maxx[i] = q[i].top(), max2[i] = -INF;
        else
        {
            max2[i] = q[i].top();
            q[i].pop();
            maxx[i] = q[i].top();
        }
    }
    
    for (int i = 1; i <= k; i++)
    {
        dp[u][i] = max(dp[u][i], down[u][i]);
    
        for (int v: X[u])
            if (v != par)
            {
                for (int j = 1; j <= i - j - 1; j++)
                {
                    long long p1 = down[v][j];
                    long long p2 = maxx[i - j - 1];
                    
                    if (p2 == down[v][i - j - 1])
                        p2 = max2[i - j - 1];
                    
                    dp[u][i] = max(dp[u][i], p1 + p2 + A[u]);
                }
            }
    }
}

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
P1161 Max path sum (Hard Version)
Contest
Brain Booster #8
Language
C++17 (G++ 13.2.0)
Submit At
2025-02-17 16:43:38
Judged At
2025-02-17 16:43:38
Judged By
Score
100
Total Time
1230ms
Peak Memory
489.43 MiB