Submission #67973027


Source Code Expand

#include <bits/stdc++.h>
#include <bits/extc++.h>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
const int maxn = 2e5+5;
long long a[maxn];
vector<int>G[maxn];
int n,k;
long long dp[maxn][7][3],dp2[maxn][7][3];

void dfs(int d,int fa)
{
    for (int i=1;i<=k;i++)
        dp[d][i][1]=a[d];
    for (auto i:G[d])
    {
        if (i!=fa)
        {
            dfs(i,d);
            for (int j=k;j>0;j--)
            {
                for (int kk=1;kk<=j;kk++)
                {
                    dp2[d][j][0]=max(dp2[d][j][0],dp[d][j-kk][0]+max({dp[i][kk][0],dp[i][kk][1],dp[i][kk][2]}));
                    dp2[d][j][2]=max(dp2[d][j][2],dp[d][j-kk][2]+max({dp[i][kk][0],dp[i][kk][1],dp[i][kk][2]}));
                    dp2[d][j][2]=max(dp2[d][j][2],dp[d][j-kk+1][1]+dp[i][kk][1]);
                    dp2[d][j][2]=max(dp2[d][j][2],dp[d][j-kk][1]+dp[i][kk+1][1]);
                    dp2[d][j][1]=max({dp2[d][j][1],dp[d][j-kk][1]+max({dp[i][kk][0],dp[i][kk][1],dp[i][kk][2]}),dp[d][j-kk][0]+dp[i][kk][1]+a[d]});
                }
            }
            for (int j=k;j>0;j--)
                for (int kk=0;kk<=2;kk++)
                    dp[d][j][kk]=max(dp[d][j][kk],dp2[d][j][kk]);
        }
    }
    for (int i=1;i<=k;i++)
        dp[d][i][1]=max(dp[d][i][1],dp[d][i-1][0]+a[d]);
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    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].push_back(v);
        G[v].push_back(u);
    }
    dfs(1,0);
    long long ans=0;
    for (int i=0;i<=k;i++)
        for (int j=0;j<3;j++)
            ans=max(ans,dp[1][i][j]);
    cout<<ans;
}

Submission Info

Submission Time
Task F - Paint Tree 2
User Alliy666
Language C++ 23 (gcc 12.2)
Score 0
Code Size 1809 Byte
Status WA
Exec Time 167 ms
Memory 97220 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 525
Status
AC × 3
AC × 13
WA × 12
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_handmade_00.txt, 01_handmade_01.txt, 02_random_00.txt, 02_random_01.txt, 02_random_02.txt, 02_random_03.txt, 02_random_04.txt, 02_random_05.txt, 02_random_06.txt, 02_random_07.txt, 02_random_08.txt, 02_random_09.txt, 02_random_10.txt, 02_random_11.txt, 02_random_12.txt, 02_random_13.txt, 02_random_14.txt, 02_random_15.txt, 02_random_16.txt, 02_random_17.txt, 02_random_18.txt, 02_random_19.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 2 ms 3452 KiB
00_sample_01.txt AC 2 ms 3468 KiB
00_sample_02.txt AC 2 ms 3472 KiB
01_handmade_00.txt AC 2 ms 3592 KiB
01_handmade_01.txt AC 2 ms 3520 KiB
02_random_00.txt WA 15 ms 13504 KiB
02_random_01.txt WA 108 ms 65148 KiB
02_random_02.txt WA 26 ms 20192 KiB
02_random_03.txt WA 25 ms 23200 KiB
02_random_04.txt WA 113 ms 67244 KiB
02_random_05.txt WA 122 ms 81716 KiB
02_random_06.txt WA 122 ms 81660 KiB
02_random_07.txt WA 148 ms 81560 KiB
02_random_08.txt WA 143 ms 81676 KiB
02_random_09.txt WA 150 ms 81772 KiB
02_random_10.txt AC 101 ms 49048 KiB
02_random_11.txt AC 101 ms 49004 KiB
02_random_12.txt AC 117 ms 97192 KiB
02_random_13.txt AC 136 ms 97220 KiB
02_random_14.txt AC 138 ms 94748 KiB
02_random_15.txt AC 167 ms 94744 KiB
02_random_16.txt AC 138 ms 96192 KiB
02_random_17.txt AC 145 ms 96260 KiB
02_random_18.txt WA 102 ms 65300 KiB
02_random_19.txt WA 142 ms 81664 KiB