#include <bits/stdc++.h>
using namespace std;
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];
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]--;
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 && l < n) {
l++;
ans++;
}
else {
if (l <= a[i]) {
l = a[i]+1;
ans++;
}
}
cout << ans << " ";
}
cout << "\n";
}
else {
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]);
//cerr << "0 " << j << " " << dp[0][j] << endl;
}
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";
}
}
Compilation message
Main.cpp:4:39: warning: overflow in conversion from 'long long int' to 'int' changes value from '9223372036854775807' to '-1' [-Woverflow]
4 | const int N = 5e5+5, M = 5e3+3, inf = LLONG_MAX;
| ^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6488 KB |
Output is correct |
6 |
Correct |
1 ms |
8540 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Incorrect |
1 ms |
10588 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
12892 KB |
Output is correct |
2 |
Incorrect |
36 ms |
49796 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6488 KB |
Output is correct |
6 |
Correct |
1 ms |
8540 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Incorrect |
1 ms |
10588 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6488 KB |
Output is correct |
6 |
Correct |
1 ms |
8540 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Incorrect |
1 ms |
10588 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
11972 KB |
Output is correct |
2 |
Incorrect |
62 ms |
11860 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6488 KB |
Output is correct |
6 |
Correct |
1 ms |
8540 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Incorrect |
1 ms |
10588 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |