Submission #239764

# Submission time Handle Problem Language Result Execution time Memory
239764 2020-06-17T09:12:25 Z VEGAnn Dostavljač (COCI18_dostavljac) C++14
140 / 140
366 ms 2552 KB
#include <bits/stdc++.h>
#define PB push_back
#define sz(x) ((int)x.size())
#define i3 array<int,3>
using namespace std;
typedef long double ld;
typedef long long ll;
const int N = 510;
const int M = 510;
const int K = 110;
const int T = 2010;
const int oo = 2e9;
vector<int> g[N];
int n, m, a[N], f[N][M][2], ans = 0;

int upd(int &x, int y){
    x = max(x, y);
}

void dfs(int v, int p){
//    for (int cur = m - 1; cur >= 0; cur--)
//        upd(f[v][cur + 1][0], f[v][cur][0] + a[v]);

    f[v][1][0] = a[v];

    for (int u : g[v]){
        if (u == p) continue;

        dfs(u, v);

        for (int cur = m; cur >= 0; cur--)
        for (int nw = m; nw >= 0; nw--) {
            if (cur + nw + 2 <= m) {
                upd(f[v][cur + nw + 2][0], f[v][cur][0] + f[u][nw][0]);
                upd(f[v][cur + nw + 2][1], f[v][cur][1] + f[u][nw][0]);
            }

            if (cur + nw + 1 <= m)
                upd(f[v][cur + nw + 1][1], f[v][cur][0] + max(f[u][nw][0], f[u][nw][1]));
        }
    }
}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);

#ifdef _LOCAL
    freopen("in.txt","r",stdin);
#endif // _LOCAL

    cin >> n >> m;

    for (int i = 0; i < n; i++)
        cin >> a[i];

    for (int i = 1; i < n; i++){
        int x, y; cin >> x >> y;
        x--; y--;
        g[x].PB(y);
        g[y].PB(x);
    }

    dfs(0, -1);

    for (int kl = 0; kl <= m; kl++) {
        upd(ans, f[0][kl][0]);
        upd(ans, f[0][kl][1]);
    }

    cout << ans;

    return 0;
}

Compilation message

dostavljac.cpp: In function 'int upd(int&, int)':
dostavljac.cpp:18:1: warning: no return statement in function returning non-void [-Wreturn-type]
 }
 ^
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 768 KB Output is correct
2 Correct 8 ms 768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 896 KB Output is correct
2 Correct 53 ms 924 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 97 ms 1148 KB Output is correct
2 Correct 35 ms 1148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 36 ms 1528 KB Output is correct
2 Correct 216 ms 1536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 191 ms 2040 KB Output is correct
2 Correct 77 ms 1912 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 366 ms 2552 KB Output is correct
2 Correct 38 ms 2424 KB Output is correct