이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define int long long
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) {
while (vis[l]) l++;
l++;
ans++;
}
else {
if (l <= a[i]) {
ans++;
}
l = max(l, a[i]+1);
}
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]);
}
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... |