답안 #987541

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
987541 2024-05-23T01:49:46 Z cig32 Fruits (NOI22_fruits) C++17
5 / 100
74 ms 16464 KB
#include "bits/stdc++.h"
#define int long long
using namespace std;
const int MAXN = 4e5 + 10;
int ps[2010][2010];
int freq(int l, int r, int lb, int ub) {
  return ps[r][ub] - ps[l-1][ub] - ps[r][lb-1] + ps[l-1][lb-1];
}
void solve(int tc) {
  int n;
  cin >> n;
  int a[n+1], c[n+1];
  for(int i=1; i<=n; i++) cin >> a[i];
  for(int i=1; i<=n; i++) cin >> c[i];


  int sumai = 0, sumci = 0;
  for(int i=1; i<=n; i++) {
    sumai += a[i];
    sumci += c[i];
  }
  if(sumai == -n) {
    int sum = 0;
    for(int i=n; i>=1; i--) {
      sum += c[i];
      cout << sum << " \n"[i == 1];
    }
    return;
  }
  if(sumci == n) {

    return;
  }
  int dp[n+1][n+1];
  for(int i=0; i<=n; i++) {
    for(int j=0; j<=n; j++) {
      dp[i][j] = -1e18;
      ps[i][j] = 0;
    }
  }
  for(int i=1; i<=n; i++) {
    for(int j=1; j<=n; j++) {
      ps[i][j] = ps[i-1][j] + ps[i][j-1] - ps[i-1][j-1] + (a[i] == j);
    }
  }
  bool chosen[n+1];
  for(int i=1; i<=n; i++) chosen[i] = 0;
  for(int i=1; i<=n; i++) if(a[i] > 0) chosen[a[i]] = 1;
  dp[0][0] = 0;
  int wow = 0;
  for(int i=1; i<=n; i++) {
    int mx = -1e18;
    for(int j=0; j<i; j++) mx = max(mx, dp[j][i-1]);
    for(int j=i; j<=n; j++) {
      if(a[i] > j || freq(i + 1, n, 1, j) > j - i) dp[j][i] = -1e18; 
      else if(a[i] == j) dp[j][i] = mx + c[j];
      else if(chosen[j]) dp[j][i] = max(dp[j][i], dp[j][i-1]);
      else if(a[i] < j && a[i] >= 1) dp[j][i] = max(dp[j][i], dp[j][i-1]);
      else {
        assert(a[i] == -1);
        dp[j][i] = mx + c[j];
        dp[j][i] = max(dp[j][i], dp[j][i-1]);
      }
      mx = max(mx, dp[j][i-1]);
    }
    
    for(int j=1; j<=n; j++) wow = max(wow, dp[j][i]);
    cout << wow << " \n"[i == n];
  }
  
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int t=1; //cin>>t;
  for(int i=1; i<=t; i++) solve(i);
}
/*
g++ T2443.cpp -std=c++17 -O2 -o T2443
./T2443 < input.txt
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Incorrect 0 ms 348 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 416 KB Output is correct
3 Correct 4 ms 968 KB Output is correct
4 Correct 47 ms 8528 KB Output is correct
5 Correct 74 ms 16464 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Incorrect 0 ms 348 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Incorrect 0 ms 348 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 36 ms 8796 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Incorrect 0 ms 348 KB Output isn't correct
4 Halted 0 ms 0 KB -