This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <stdio.h>
#define N 5000
#define M 5000
#define K 1000
#define INF 0x3f3f3f3f
int max(int a, int b) { return a > b ? a : b; }
int main() {
static int aa[N], bb[M], ww[K], dp[M + 1][2][2], dq[M + 1][2][2];
int n, m, k, a, b, i, j, c, u, v, ans;
scanf("%d%d%d%d%d", &k, &n, &m, &a, &b);
for (c = 0; c < k; c++)
scanf("%d", &ww[c]);
for (i = 0; i < n; i++)
scanf("%d", &aa[i]), aa[i]--;
for (j = 0; j < m; j++)
scanf("%d", &bb[j]), bb[j]--;
for (j = 0; j <= m; j++)
for (u = 0; u < 2; u++)
for (v = 0; v < 2; v++)
dp[j][u][v] = -INF;
ans = -INF;
for (i = 0; i <= n; i++) {
for (j = 0; j <= m; j++)
for (u = 0; u < 2; u++)
for (v = 0; v < 2; v++)
dq[j][u][v] = -INF;
dp[0][1][1] = max(dp[0][1][1], 0);
for (j = 0; j <= m; j++)
for (u = 0; u < 2; u++)
for (v = 0; v < 2; v++) {
int x = dp[j][u][v];
if (x == -INF)
continue;
if (i < n)
dq[j][0][v] = max(dq[j][0][v], x + (u == 1 ? a : 0) + b);
if (j < m)
dp[j + 1][u][0] = max(dp[j + 1][u][0], x + (v == 1 ? a : 0) + b);
if (i < n && j < m && aa[i] == bb[j])
dq[j + 1][1][1] = max(dq[j + 1][1][1], x + ww[aa[i]]);
}
for (u = 0; u < 2; u++)
for (v = 0; v < 2; v++)
ans = max(ans, dp[m][u][v]);
for (j = 0; j <= m; j++)
for (u = 0; u < 2; u++)
for (v = 0; v < 2; v++)
dp[j][u][v] = dq[j][u][v];
}
printf("%d\n", ans);
return 0;
}
Compilation message (stderr)
VisitingSingapore.c: In function 'main':
VisitingSingapore.c:14:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
14 | scanf("%d%d%d%d%d", &k, &n, &m, &a, &b);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
VisitingSingapore.c:16:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
16 | scanf("%d", &ww[c]);
| ^~~~~~~~~~~~~~~~~~~
VisitingSingapore.c:18:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
18 | scanf("%d", &aa[i]), aa[i]--;
| ^~~~~~~~~~~~~~~~~~~
VisitingSingapore.c:20:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
20 | scanf("%d", &bb[j]), bb[j]--;
| ^~~~~~~~~~~~~~~~~~~
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |