이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <stdio.h>
#define N	400000
long long max(long long a, long long b) { return a > b ? a : b; }
int main() {
	static int aa[N], cc[N];
	static long long ss[N + 1];
	static char used[N];
	static int kk[N + 1], xx[N], yy[N], dd[N];
	static long long dp[N];
	int n, k, i, j, l, a, x, y, d;
	long long z;
	scanf("%d", &n);
	for (i = 0; i < n; i++) {
		scanf("%d", &aa[i]), aa[i]--;
		if (aa[i] >= 0)
			used[aa[i]] = 1;
	}
	for (a = 0; a < n; a++)
		scanf("%d", &cc[a]);
	k = 0;
	for (a = 0; a < n; a++) {
		kk[a] = k;
		if (!used[a])
			ss[k + 1] = ss[k] + cc[a], k++;
	}
	kk[n] = k;
	if (k == n) {
		for (i = 0; i < n; i++)
			printf("%lld ", ss[n] - ss[n - 1 - i]);
		printf("\n");
		return 0;
	}
	l = 0, a = -1;
	for (j = 0; j < n; j++) {
		if (aa[j] == -2)
			l++;
		else if (a > aa[j])
			aa[j] = -1;
		else {
			a = aa[j];
			xx[j] = kk[aa[j]], yy[j] = l, dd[j] = xx[j] - yy[j];
			if (dd[j] < 0)
				aa[j] = -1;
		}
		if (aa[j] >= 0) {
			dp[j] = 0;
			for (i = j - 1; i >= 0; i--) {
				if (aa[i] < 0)
					continue;
				if (dd[i] >= dd[j])
					break;
				dp[j] = max(dp[j], dp[i] + ss[xx[j]] - ss[yy[i] + dd[j]]);
			}
			if (i >= 0)
				dp[j] = max(dp[j], dp[i] + ss[xx[j]] - ss[xx[i]]);
			else
				dp[j] = max(dp[j], ss[xx[j]] - ss[dd[j]]);
			dp[j] += cc[aa[j]];
		}
		x = kk[n], y = l, d = kk[n] - y;
		z = 0;
		for (i = j; i >= 0; i--) {
			if (aa[i] < 0)
				continue;
			if (dd[i] >= d)
				break;
			z = max(z, dp[i] + ss[x] - ss[yy[i] + d]);
		}
		if (i >= 0)
			z = max(z, dp[i] + ss[x] - ss[xx[i]]);
		else
			z = max(z, ss[x] - ss[d]);
		printf("%lld ", z);
	}
	printf("\n");
	return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
Main.c: In function 'main':
Main.c:16:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   16 |  scanf("%d", &n);
      |  ^~~~~~~~~~~~~~~
Main.c:18:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   18 |   scanf("%d", &aa[i]), aa[i]--;
      |   ^~~~~~~~~~~~~~~~~~~
Main.c:23:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   23 |   scanf("%d", &cc[a]);
      |   ^~~~~~~~~~~~~~~~~~~| # | 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... |