Submission #481422

# Submission time Handle Problem Language Result Execution time Memory
481422 2021-10-20T18:17:43 Z Edbert2397 Biochips (IZhO12_biochips) C++14
100 / 100
397 ms 414920 KB
# include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
typedef long long ll;
#define pii pair<int,int>
#define debug(x) cout << #x << '=' << x << endl;
const int N = 2e5 + 5;
const int INF = 1e9;
const ll mod = 1e9+7;
int n,m,x[N],now,sz,dp[N][505],z,nilai[N][105],subtree[N],in[N],out[N],cnt;
vector<int >v[N];
vector<pii >vec[N];
void dfs(int idx){
    in[idx] = ++cnt;
    for(auto isi : v[idx]){
        dfs(isi);
    }
    out[idx] = cnt;
} 
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    //freopen(,r,stdin);
    //freopen(,w,stdout);
    cin>>n>>m;
    for(int i = 1;i<=n;i++){
        int p;
        cin>>p>>x[i];
        v[p].pb(i);
    }
    for(auto isi : v[0]) dfs(isi);
    for(int i = 1;i<=n;i++){
        vec[out[i]].pb({in[i],x[i]});
    }
    for(int i = 0;i<=n;i++){
        for(int j = 1;j<=m;j++){
            dp[i][i] = -INF;

        }
    }
    for(int i = 1;i<=n;i++){
        for(int j = 1;j<=m;j++){
            dp[i][j] = dp[i-1][j];
            for(auto isi : vec[i]){
                dp[i][j] = max(dp[i][j],dp[isi.fi-1][j-1] + isi.se);
            }
        }

    }
    
    cout<<dp[n][m]<<endl;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9676 KB Output is correct
2 Correct 4 ms 9676 KB Output is correct
3 Correct 4 ms 9884 KB Output is correct
4 Correct 15 ms 27772 KB Output is correct
5 Correct 16 ms 29980 KB Output is correct
6 Correct 17 ms 30084 KB Output is correct
7 Correct 264 ms 310596 KB Output is correct
8 Correct 253 ms 310520 KB Output is correct
9 Correct 326 ms 376244 KB Output is correct
10 Correct 397 ms 414920 KB Output is correct