Submission #1086149

# Submission time Handle Problem Language Result Execution time Memory
1086149 2024-09-09T15:58:06 Z TrinhKhanhDung Biochips (IZhO12_biochips) C++14
30 / 100
2000 ms 524292 KB
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define sz(x) (int)x.size()
#define ALL(v) v.begin(),v.end()
#define MASK(k) (1LL << (k))
#define BIT(x, i) (((x) >> (i)) & 1)
#define oo (ll)1e18
#define INF (ll)1e9
#define MOD (ll)(998244353)

using namespace std;

template<class T1, class T2>
    bool maximize(T1 &a, T2 b){if(a < b){a = b; return true;} return false;}

template<class T1, class T2>
    bool minimize(T1 &a, T2 b){if(a > b){a = b; return true;} return false;}

template<class T1, class T2>
    void add(T1 &a, T2 b){a += b; if(a >= MOD) a -= MOD;}

template<class T1, class T2>
    void sub(T1 &a, T2 b){a -= b; if(a < 0) a += MOD;}

template<class T>
    void cps(T &v){sort(ALL(v)); v.resize(unique(ALL(v)) - v.begin());}

const int MAXN = 2e5 + 1, MAXK = 501;

int N, K;
vector<int> adj[MAXN];
int value[MAXK], cnt[MAXN];
int f[MAXN][MAXK];

void dfs(int u) {
    f[u][0] = 0;
    cnt[u] = 1;
    for (auto v : adj[u]) {
        dfs(v);
        cnt[u] += cnt[v];

        for (int i = min(cnt[u], K); i >= 1; i--) {
            for (int j = max(0, i - cnt[u] + cnt[v]); j <= cnt[v] && j <= i; j++) {
                if (f[v][j] == -1)
                    continue;

                f[u][i] = max(f[u][i], f[v][j] + f[u][i - j]);
            }
        }
    }

    f[u][1] = max(f[u][1], value[u]);
}

void solve(){
    cin >> N >> K;
    memset(f, -1, sizeof f);
    int root = 0;
    for(int i=1; i<=N; i++){
        int p; cin >> p >> value[i];
        if(p != 0) adj[p].push_back(i);
        else root = i;
        f[i][0] = 0;
    }
    dfs(root);
    cout << f[root][K] << '\n';
}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
//    freopen("numtable.inp","r",stdin);
//    freopen("numtable.out","w",stdout);

    solve();

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 202 ms 397140 KB Output is correct
2 Correct 183 ms 397140 KB Output is correct
3 Correct 184 ms 397088 KB Output is correct
4 Runtime error 744 ms 524288 KB Execution killed with signal 9
5 Execution timed out 2614 ms 524288 KB Time limit exceeded
6 Execution timed out 2416 ms 524288 KB Time limit exceeded
7 Execution timed out 2395 ms 524288 KB Time limit exceeded
8 Execution timed out 2383 ms 524288 KB Time limit exceeded
9 Execution timed out 2399 ms 524292 KB Time limit exceeded
10 Execution timed out 2392 ms 524288 KB Time limit exceeded