#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define int long long
#define pii pair<int, int>
#define ff first
#define ss second
#define entire(x) (x).begin(), (x).end()
int32_t main(){
ios::sync_with_stdio(false);
int n; cin >> n;
vector<int> a(n), c(n);
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n; i++) cin >> c[i];
vector<bool> vis(n, false);
for (int i = 0; i < n; i++){
if (a[i] != -1) vis[--a[i]] = true;
}
vector<vector<int>> dp(n, vector<int>(n, 0));
if (a[0] == -1){
for (int i = 0; i < n; i++) if (!vis[i]) dp[0][i] = c[i];
} else {
dp[0][a[0]] = c[a[0]];
for (int i = 0; i < a[0]; i++) vis[i] = true;
}
for (int i = 1; i < n; i++){
if (a[i] == -1){
int pref = 0;
for (int j = 0; j < n; j++) {
if (!vis[j]) dp[i][j] = max(dp[i-1][j], pref + c[j]);
pref = max(pref, dp[i-1][j]);
}
} else {
int pref = 0;
for (int j = 0; j < a[i]; j++) pref = max(pref, dp[i-1][j]), vis[j] = true;
dp[i][a[i]] = pref + c[a[i]];
for (int j = a[i] + 1; j < n; j++) dp[i][j] = dp[i-1][j];
}
}
for (int i = 0; i < n; i++){
int ans = 0;
for (int j = 0; j < n; j++) ans = max(ans, dp[i][j]);
cout << ans << " ";
}
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... |