#include <bits/stdc++.h>
using namespace std;
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
int a[n],c[n];
bool fix[n];
fill(fix,fix+n,0);
for(int &i : a){
cin >> i;
if(i!=-1)
i--;
fix[i]=1;
}
for(int &i : c){
cin >> i;
}
int dp[n][n];
//maxind,maxval
int temp=0;
for(int i = 0;i<n;i++){
if(a[0]==-1){
if(!fix[i]){
temp=c[i];
}
dp[0][i]=temp;
}
else if(a[0]<=i){
dp[0][i]=c[a[i]];
}
else{
dp[0][i]=0;
}
}
int prefmx[n];
prefmx[0]=a[0];
fill(prefmx,prefmx+n,-2);
for(int i = 1;i<n;i++){
prefmx[i]=max(prefmx[i],a[i]);
}
cout << dp[0][n-1] << " ";
for(int i = 1;i<n;i++){
dp[i][0]=dp[i-1][0];
if(a[i]==-1){
dp[i][0]=max(dp[i][0],c[0]);
}
if(0<prefmx[i]){
dp[i][0]=0;
}
for(int mx = 1;mx<n;mx++){
dp[i][mx]=max({dp[i-1][mx],dp[i][mx-1]});
if(a[i]==-1&&!fix[mx]){
dp[i][mx]=max({dp[i-1][mx],dp[i-1][mx-1]+c[mx],dp[i][mx-1]});
}
if(a[i]==mx){
dp[i][mx]=max({dp[i-1][mx],dp[i-1][mx-1]+c[mx],dp[i][mx-1]});
}
if(mx<prefmx[i]){
dp[i][mx]=0;
}
// cout << i << " " << mx << " " << dp[i][mx] << "\n";
}
cout << dp[i][n-1] << " ";
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |