#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";
}
}*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
2 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
2 ms |
6492 KB |
Output is correct |
3 |
Correct |
4 ms |
6808 KB |
Output is correct |
4 |
Correct |
37 ms |
11212 KB |
Output is correct |
5 |
Correct |
71 ms |
17248 KB |
Output is correct |
6 |
Correct |
1 ms |
6488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
2 ms |
6492 KB |
Output is correct |
11 |
Execution timed out |
1037 ms |
6492 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
2 ms |
6492 KB |
Output is correct |
11 |
Execution timed out |
1037 ms |
6492 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1046 ms |
14940 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
2 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
2 ms |
6492 KB |
Output is correct |
13 |
Correct |
4 ms |
6808 KB |
Output is correct |
14 |
Correct |
37 ms |
11212 KB |
Output is correct |
15 |
Correct |
71 ms |
17248 KB |
Output is correct |
16 |
Correct |
1 ms |
6488 KB |
Output is correct |
17 |
Execution timed out |
1037 ms |
6492 KB |
Time limit exceeded |
18 |
Halted |
0 ms |
0 KB |
- |