This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(x) x.begin(), x.end()
const int N = 5e5+5, M = 5e3+3, inf = LLONG_MAX;
int n, mxA, mxC, a[N], c[N], pre[N], preDp[N], dp[M][M], Ans[N];
bitset<N> vis;
signed main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
cin >> n;
mxA = mxC = -inf;
for (int i = 0; i < n; i++) {
cin >> a[i];
if (a[i] != -1) {
a[i]--;
//assert(!vis[a[i]]);
vis[a[i]] = 1;
}
mxA = max(mxA, a[i]);
}
for (int i = 0; i < n; i++) {
cin >> c[i];
mxC = max(mxC, c[i]);
}
for (int i = 0; i < n; i++) {
pre[i] = max((i ? pre[i-1] : -1), a[i]);
}
if (mxA == -1) {
int sum = 0;
for (int i = n-1; i >= 0; i--) {
sum += c[i];
cout << sum << " ";
}
cout << "\n";
}/*
else if (mxC == 1) {
int l = 0;
int ans = 0;
for (int i = 0; i < n; i++) {
if (a[i] == -1) {
while (vis[l]) l++;
if (l < n) {
l++;
ans++;
}
}
else {
if (l <= a[i]) {
ans++;
}
l = max(l, a[i]+1);
}
cout << ans << " ";
}
cout << "\n";
}*/
else {
vector<int> v(n);
for (int i = 0; i < n; i++) v[i] = i;
do {
int x = 0;
int mx = -1;
for (int i = 0; i < n; i++) {
if ((vis[v[i]] || a[i] != -1) && a[i] != v[i]) break;
if (mx < v[i]) {
x += c[v[i]];
mx = v[i];
}
Ans[i] = max(Ans[i], x);
}
}
while (next_permutation(all(v)));
for (int i = 0; i < n; i++) {
cout << Ans[i] << " ";
}
cout << "\n";
}
}/*
for (int j = 0; j < n; j++) {
if ((a[0] == -1 && !vis[j]) || a[0] == j) dp[0][j] = c[j];
else dp[0][j] = 0;
preDp[j] = max((j ? preDp[j-1] : 0), dp[0][j]);
}
for (int i = 1; i < n; i++) {
for (int j = 0; j < n; j++) {
// dp[i][j] = max coste con los i primeros donde el mas grande es j
dp[i][j] = 0;
if (pre[i] > j) continue;
if (a[i] == -1) {
if (pre[i] < j) {
if (!vis[j]) dp[i][j] = max(dp[i-1][j], (j ? preDp[j-1] : 0) + c[j]);
}
else {
dp[i][j] = dp[i-1][j];
}
}
else {
if (a[i] == j) dp[i][j] = (j ? preDp[j-1] : 0) + c[j];
else {
dp[i][j] = dp[i-1][j];
}
}//cerr << i << " " << j << " " << dp[i][j] << endl;
}
for (int j = 0; j < n; j++) {
preDp[j] = max((j ? preDp[j-1] : 0), dp[i][j]);
}
}
for (int i = 0; i < n; i++) {
int ans = 0;
for (int j = pre[i]; j < n; j++) {
ans = max(ans, dp[i][j]);
}
cout << ans << " ";
}
cout << "\n";
}
}*/
# | 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... |