Submission #967498

# Submission time Handle Problem Language Result Execution time Memory
967498 2024-04-22T09:08:44 Z vjudge1 Dostavljač (COCI18_dostavljac) C++17
140 / 140
331 ms 4300 KB
#include <bits/stdc++.h>
#define int long long
using namespace std;

#define ll long long
#define ld long double
#define ull unsigned long long
#define pii pair<int,int>
#define pll pair<long long, long long>
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define lwb lower_bound
#define upb upper_bound

#define TASKNAME "NAME"

void init()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    ///freopen(TASKNAME".INP","r",stdin); freopen(TASKNAME".OUT","w",stdout);
}

const int SZ = 5e2+5;
const ll INF = INT_MAX / 2, MOD = 1e9+7, INFLL = 2e18;
const double epsilon = 1e-3;

int n,m, a[SZ], dp[SZ][SZ][2];
vector<int> adj[SZ];

void dfs(int u, int pre)
{
    for(int v : adj[u])
    {
        if(v == pre) continue;
        dfs(v, u);
        vector<int> tem[2];
        tem[0].resize(m + 1, 0);
        tem[1].resize(m + 1, 0);
        for(int j = 0; j <= m; j++)
        {
            for(int k = 0; k < j; k++)
            {
                if(j - k > 1)
                {
                    tem[0][j] = max(tem[0][j], dp[u][j - k - 2][0] + dp[v][k][0]);
                    tem[1][j] = max(tem[1][j], dp[u][j - k - 2][1] + dp[v][k][0]);
                }
                tem[1][j] = max(tem[1][j], dp[u][j - k - 1][0] + dp[v][k][1]);
            }
        }
        for (int j = 0; j <= m; j++)
        {
            dp[u][j][0] = max(dp[u][j][0], tem[0][j]);
            dp[u][j][1] = max(dp[u][j][1], tem[1][j]);
        }
    }
    for (int j = m; j >= 1; j--){
        dp[u][j][0] = max(dp[u][j][0], dp[u][j - 1][0] + a[u]);
        dp[u][j][1] = max(dp[u][j][1], dp[u][j - 1][1] + a[u]);
    }
}

signed main()
{
    init();
    cin >> n >> m;
    for(int i = 1; i <= n; i++)
    {
        cin >> a[i];
    }
    for(int i = 1; i < n; i++)
    {
        int u,v;
        cin >> u >> v;
        adj[u].pb(v);
        adj[v].pb(u);
    }
    dfs(1, 0);
    int res = 0;
    for(int i = 0; i <= m; i++)
    {
        res = max(res, dp[1][i][1]);
    }
    cout << res;
}

# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 1116 KB Output is correct
2 Correct 3 ms 1044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1116 KB Output is correct
2 Correct 48 ms 1576 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 86 ms 1900 KB Output is correct
2 Correct 26 ms 1872 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 2396 KB Output is correct
2 Correct 198 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 170 ms 3428 KB Output is correct
2 Correct 63 ms 3412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 331 ms 4300 KB Output is correct
2 Correct 31 ms 3408 KB Output is correct