#pragma GCC optimize("O2")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FOD(i, a, b) for (int i = (a); i >= (b); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(x) (x).begin(), (x).end()
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
#define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
#define int long long
using namespace std;
const int MAXN = 5005;
const long long INF = 0x3f3f3f3f3f3f3f3f;
int K, N, M, v[MAXN], s[MAXN], t[MAXN];
long long dp[MAXN], Max[MAXN][MAXN], Ord[MAXN], prv[MAXN];
long long A, B;
signed main(void) {
ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
//file("TASK");
cin >> K >> N >> M >> A >> B;
FOR(i, 1, K) cin >> v[i];
FOR(i, 1, N) cin >> s[i];
FOR(i, 1, M) cin >> t[i];
memset(dp, -INF, sizeof dp);
memset(Max, -INF, sizeof Max);
memset(Ord, -INF, sizeof Ord);
dp[0] = 0;
long long ans = A + M * B;
FOR(i, 1, N) {
FOR(j, 0, M) prv[j] = dp[j];
FOR(j, 0, M) dp[j] = -INF;
long long cur = -INF;
FOR(j, 1, M) {
if (s[i] == t[j]) {
dp[j] = prv[j - 1] + v[t[j]];
if (i >= 2 && j >= 2)
dp[j] = max(dp[j], Max[i - 2][j - 2] + v[t[j]] + B * (i + j) + 2 * (A - B));
if (j >= 2)
dp[j] = max(dp[j], cur + v[t[j]] + B * j + A - B);
if (i >= 2)
dp[j] = max(dp[j], Ord[j - 1] + v[t[j]] + B * i + A - B);
dp[j] = max(dp[j], v[t[j]] + (j > 1) * A + (j - 1) * B);
}
Max[i][j] = dp[j] - B *(i + j);
Max[i][j] = max(Max[i][j], Max[i - 1][j]);
Max[i][j] = max(Max[i][j], Max[i][j - 1]);
if (j >= 1) cur = max(cur, prv[j - 1] - (j - 1) * B);
//Ord[i][j] = dp[i][j] - i * B;
//Ord[i][j] = max(Ord[i][j], Ord[i - 1][j]);
if (dp[j] != -INF)
ans = max(ans, dp[j] + (M > j) * A + (M - j) * B);
}
if (i >= 1) {
FOR(j, 1, M) {
Ord[j] = max(Ord[j], prv[j] - (i - 1) * B);
}
}
}
//FOR(i, 1, N) FOR(j, 1, M) cout << dp[i][j] << " \n" [j == M];
cout << ans;
cerr << "Time elapsed: " << TIME << " s.\n";
return (0 ^ 0);
}
Compilation message
VisitingSingapore.cpp: In function 'int main()':
VisitingSingapore.cpp:28:16: warning: overflow in conversion from 'long long int' to 'int' changes value from '-4557430888798830399' to '-1061109567' [-Woverflow]
28 | memset(dp, -INF, sizeof dp);
| ^~~~
VisitingSingapore.cpp:29:17: warning: overflow in conversion from 'long long int' to 'int' changes value from '-4557430888798830399' to '-1061109567' [-Woverflow]
29 | memset(Max, -INF, sizeof Max);
| ^~~~
VisitingSingapore.cpp:30:17: warning: overflow in conversion from 'long long int' to 'int' changes value from '-4557430888798830399' to '-1061109567' [-Woverflow]
30 | memset(Ord, -INF, sizeof Ord);
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
87 ms |
196440 KB |
Output is correct |
2 |
Correct |
81 ms |
196476 KB |
Output is correct |
3 |
Correct |
71 ms |
196452 KB |
Output is correct |
4 |
Correct |
70 ms |
196392 KB |
Output is correct |
5 |
Correct |
69 ms |
196420 KB |
Output is correct |
6 |
Correct |
87 ms |
196412 KB |
Output is correct |
7 |
Correct |
71 ms |
196360 KB |
Output is correct |
8 |
Correct |
72 ms |
196592 KB |
Output is correct |
9 |
Correct |
76 ms |
196464 KB |
Output is correct |
10 |
Correct |
81 ms |
196464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
70 ms |
196384 KB |
Output is correct |
2 |
Correct |
83 ms |
196476 KB |
Output is correct |
3 |
Correct |
87 ms |
196440 KB |
Output is correct |
4 |
Correct |
76 ms |
196376 KB |
Output is correct |
5 |
Correct |
76 ms |
196392 KB |
Output is correct |
6 |
Correct |
77 ms |
196420 KB |
Output is correct |
7 |
Correct |
77 ms |
196556 KB |
Output is correct |
8 |
Correct |
79 ms |
196476 KB |
Output is correct |
9 |
Correct |
82 ms |
196476 KB |
Output is correct |
10 |
Correct |
83 ms |
196428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
85 ms |
196428 KB |
Output is correct |
2 |
Correct |
90 ms |
196412 KB |
Output is correct |
3 |
Correct |
120 ms |
196456 KB |
Output is correct |
4 |
Correct |
168 ms |
196600 KB |
Output is correct |
5 |
Correct |
103 ms |
196496 KB |
Output is correct |
6 |
Correct |
119 ms |
196556 KB |
Output is correct |
7 |
Correct |
158 ms |
196580 KB |
Output is correct |
8 |
Correct |
96 ms |
196536 KB |
Output is correct |
9 |
Correct |
121 ms |
196572 KB |
Output is correct |
10 |
Correct |
192 ms |
196588 KB |
Output is correct |
11 |
Correct |
191 ms |
196556 KB |
Output is correct |
12 |
Correct |
189 ms |
196532 KB |
Output is correct |
13 |
Correct |
212 ms |
196600 KB |
Output is correct |
14 |
Correct |
342 ms |
196564 KB |
Output is correct |
15 |
Correct |
336 ms |
196620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
85 ms |
196428 KB |
Output is correct |
2 |
Correct |
90 ms |
196412 KB |
Output is correct |
3 |
Correct |
120 ms |
196456 KB |
Output is correct |
4 |
Correct |
168 ms |
196600 KB |
Output is correct |
5 |
Correct |
103 ms |
196496 KB |
Output is correct |
6 |
Correct |
119 ms |
196556 KB |
Output is correct |
7 |
Correct |
158 ms |
196580 KB |
Output is correct |
8 |
Correct |
96 ms |
196536 KB |
Output is correct |
9 |
Correct |
121 ms |
196572 KB |
Output is correct |
10 |
Correct |
192 ms |
196588 KB |
Output is correct |
11 |
Correct |
191 ms |
196556 KB |
Output is correct |
12 |
Correct |
189 ms |
196532 KB |
Output is correct |
13 |
Correct |
212 ms |
196600 KB |
Output is correct |
14 |
Correct |
342 ms |
196564 KB |
Output is correct |
15 |
Correct |
336 ms |
196620 KB |
Output is correct |
16 |
Correct |
166 ms |
196600 KB |
Output is correct |
17 |
Correct |
146 ms |
196500 KB |
Output is correct |
18 |
Correct |
149 ms |
196600 KB |
Output is correct |
19 |
Correct |
88 ms |
196544 KB |
Output is correct |
20 |
Correct |
147 ms |
196572 KB |
Output is correct |
21 |
Correct |
74 ms |
196400 KB |
Output is correct |
22 |
Correct |
86 ms |
196480 KB |
Output is correct |
23 |
Correct |
110 ms |
196576 KB |
Output is correct |
24 |
Correct |
87 ms |
196528 KB |
Output is correct |
25 |
Correct |
194 ms |
196636 KB |
Output is correct |
26 |
Correct |
241 ms |
196636 KB |
Output is correct |
27 |
Correct |
198 ms |
196624 KB |
Output is correct |
28 |
Correct |
198 ms |
196620 KB |
Output is correct |
29 |
Correct |
387 ms |
196612 KB |
Output is correct |
30 |
Correct |
325 ms |
196612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
85 ms |
196428 KB |
Output is correct |
2 |
Correct |
90 ms |
196412 KB |
Output is correct |
3 |
Correct |
120 ms |
196456 KB |
Output is correct |
4 |
Correct |
168 ms |
196600 KB |
Output is correct |
5 |
Correct |
103 ms |
196496 KB |
Output is correct |
6 |
Correct |
119 ms |
196556 KB |
Output is correct |
7 |
Correct |
158 ms |
196580 KB |
Output is correct |
8 |
Correct |
96 ms |
196536 KB |
Output is correct |
9 |
Correct |
121 ms |
196572 KB |
Output is correct |
10 |
Correct |
192 ms |
196588 KB |
Output is correct |
11 |
Correct |
191 ms |
196556 KB |
Output is correct |
12 |
Correct |
189 ms |
196532 KB |
Output is correct |
13 |
Correct |
212 ms |
196600 KB |
Output is correct |
14 |
Correct |
342 ms |
196564 KB |
Output is correct |
15 |
Correct |
336 ms |
196620 KB |
Output is correct |
16 |
Correct |
204 ms |
196524 KB |
Output is correct |
17 |
Correct |
134 ms |
196556 KB |
Output is correct |
18 |
Correct |
90 ms |
196428 KB |
Output is correct |
19 |
Correct |
75 ms |
196396 KB |
Output is correct |
20 |
Correct |
73 ms |
196504 KB |
Output is correct |
21 |
Correct |
79 ms |
196492 KB |
Output is correct |
22 |
Correct |
139 ms |
196588 KB |
Output is correct |
23 |
Correct |
97 ms |
196520 KB |
Output is correct |
24 |
Correct |
160 ms |
196556 KB |
Output is correct |
25 |
Correct |
219 ms |
196640 KB |
Output is correct |
26 |
Correct |
218 ms |
196620 KB |
Output is correct |
27 |
Correct |
207 ms |
196516 KB |
Output is correct |
28 |
Correct |
213 ms |
196624 KB |
Output is correct |
29 |
Correct |
363 ms |
196620 KB |
Output is correct |
30 |
Correct |
378 ms |
196612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
83 ms |
196452 KB |
Output is correct |
2 |
Correct |
89 ms |
196428 KB |
Output is correct |
3 |
Correct |
86 ms |
196356 KB |
Output is correct |
4 |
Correct |
84 ms |
196484 KB |
Output is correct |
5 |
Correct |
84 ms |
196480 KB |
Output is correct |
6 |
Correct |
84 ms |
196448 KB |
Output is correct |
7 |
Correct |
81 ms |
196376 KB |
Output is correct |
8 |
Correct |
79 ms |
196424 KB |
Output is correct |
9 |
Correct |
89 ms |
196400 KB |
Output is correct |
10 |
Correct |
81 ms |
196400 KB |
Output is correct |
11 |
Correct |
82 ms |
196428 KB |
Output is correct |
12 |
Correct |
77 ms |
196420 KB |
Output is correct |
13 |
Correct |
82 ms |
196480 KB |
Output is correct |
14 |
Correct |
80 ms |
196380 KB |
Output is correct |
15 |
Correct |
82 ms |
196424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
87 ms |
196440 KB |
Output is correct |
2 |
Correct |
81 ms |
196476 KB |
Output is correct |
3 |
Correct |
71 ms |
196452 KB |
Output is correct |
4 |
Correct |
70 ms |
196392 KB |
Output is correct |
5 |
Correct |
69 ms |
196420 KB |
Output is correct |
6 |
Correct |
87 ms |
196412 KB |
Output is correct |
7 |
Correct |
71 ms |
196360 KB |
Output is correct |
8 |
Correct |
72 ms |
196592 KB |
Output is correct |
9 |
Correct |
76 ms |
196464 KB |
Output is correct |
10 |
Correct |
81 ms |
196464 KB |
Output is correct |
11 |
Correct |
70 ms |
196384 KB |
Output is correct |
12 |
Correct |
83 ms |
196476 KB |
Output is correct |
13 |
Correct |
87 ms |
196440 KB |
Output is correct |
14 |
Correct |
76 ms |
196376 KB |
Output is correct |
15 |
Correct |
76 ms |
196392 KB |
Output is correct |
16 |
Correct |
77 ms |
196420 KB |
Output is correct |
17 |
Correct |
77 ms |
196556 KB |
Output is correct |
18 |
Correct |
79 ms |
196476 KB |
Output is correct |
19 |
Correct |
82 ms |
196476 KB |
Output is correct |
20 |
Correct |
83 ms |
196428 KB |
Output is correct |
21 |
Correct |
85 ms |
196428 KB |
Output is correct |
22 |
Correct |
90 ms |
196412 KB |
Output is correct |
23 |
Correct |
120 ms |
196456 KB |
Output is correct |
24 |
Correct |
168 ms |
196600 KB |
Output is correct |
25 |
Correct |
103 ms |
196496 KB |
Output is correct |
26 |
Correct |
119 ms |
196556 KB |
Output is correct |
27 |
Correct |
158 ms |
196580 KB |
Output is correct |
28 |
Correct |
96 ms |
196536 KB |
Output is correct |
29 |
Correct |
121 ms |
196572 KB |
Output is correct |
30 |
Correct |
192 ms |
196588 KB |
Output is correct |
31 |
Correct |
191 ms |
196556 KB |
Output is correct |
32 |
Correct |
189 ms |
196532 KB |
Output is correct |
33 |
Correct |
212 ms |
196600 KB |
Output is correct |
34 |
Correct |
342 ms |
196564 KB |
Output is correct |
35 |
Correct |
336 ms |
196620 KB |
Output is correct |
36 |
Correct |
166 ms |
196600 KB |
Output is correct |
37 |
Correct |
146 ms |
196500 KB |
Output is correct |
38 |
Correct |
149 ms |
196600 KB |
Output is correct |
39 |
Correct |
88 ms |
196544 KB |
Output is correct |
40 |
Correct |
147 ms |
196572 KB |
Output is correct |
41 |
Correct |
74 ms |
196400 KB |
Output is correct |
42 |
Correct |
86 ms |
196480 KB |
Output is correct |
43 |
Correct |
110 ms |
196576 KB |
Output is correct |
44 |
Correct |
87 ms |
196528 KB |
Output is correct |
45 |
Correct |
194 ms |
196636 KB |
Output is correct |
46 |
Correct |
241 ms |
196636 KB |
Output is correct |
47 |
Correct |
198 ms |
196624 KB |
Output is correct |
48 |
Correct |
198 ms |
196620 KB |
Output is correct |
49 |
Correct |
387 ms |
196612 KB |
Output is correct |
50 |
Correct |
325 ms |
196612 KB |
Output is correct |
51 |
Correct |
204 ms |
196524 KB |
Output is correct |
52 |
Correct |
134 ms |
196556 KB |
Output is correct |
53 |
Correct |
90 ms |
196428 KB |
Output is correct |
54 |
Correct |
75 ms |
196396 KB |
Output is correct |
55 |
Correct |
73 ms |
196504 KB |
Output is correct |
56 |
Correct |
79 ms |
196492 KB |
Output is correct |
57 |
Correct |
139 ms |
196588 KB |
Output is correct |
58 |
Correct |
97 ms |
196520 KB |
Output is correct |
59 |
Correct |
160 ms |
196556 KB |
Output is correct |
60 |
Correct |
219 ms |
196640 KB |
Output is correct |
61 |
Correct |
218 ms |
196620 KB |
Output is correct |
62 |
Correct |
207 ms |
196516 KB |
Output is correct |
63 |
Correct |
213 ms |
196624 KB |
Output is correct |
64 |
Correct |
363 ms |
196620 KB |
Output is correct |
65 |
Correct |
378 ms |
196612 KB |
Output is correct |
66 |
Correct |
83 ms |
196452 KB |
Output is correct |
67 |
Correct |
89 ms |
196428 KB |
Output is correct |
68 |
Correct |
86 ms |
196356 KB |
Output is correct |
69 |
Correct |
84 ms |
196484 KB |
Output is correct |
70 |
Correct |
84 ms |
196480 KB |
Output is correct |
71 |
Correct |
84 ms |
196448 KB |
Output is correct |
72 |
Correct |
81 ms |
196376 KB |
Output is correct |
73 |
Correct |
79 ms |
196424 KB |
Output is correct |
74 |
Correct |
89 ms |
196400 KB |
Output is correct |
75 |
Correct |
81 ms |
196400 KB |
Output is correct |
76 |
Correct |
82 ms |
196428 KB |
Output is correct |
77 |
Correct |
77 ms |
196420 KB |
Output is correct |
78 |
Correct |
82 ms |
196480 KB |
Output is correct |
79 |
Correct |
80 ms |
196380 KB |
Output is correct |
80 |
Correct |
82 ms |
196424 KB |
Output is correct |
81 |
Correct |
188 ms |
196624 KB |
Output is correct |
82 |
Correct |
102 ms |
196552 KB |
Output is correct |
83 |
Correct |
120 ms |
196592 KB |
Output is correct |
84 |
Correct |
103 ms |
196568 KB |
Output is correct |
85 |
Correct |
138 ms |
196516 KB |
Output is correct |
86 |
Correct |
126 ms |
196568 KB |
Output is correct |
87 |
Correct |
101 ms |
196432 KB |
Output is correct |
88 |
Correct |
86 ms |
196460 KB |
Output is correct |
89 |
Correct |
95 ms |
196536 KB |
Output is correct |
90 |
Correct |
272 ms |
196608 KB |
Output is correct |
91 |
Correct |
216 ms |
196548 KB |
Output is correct |
92 |
Correct |
208 ms |
196632 KB |
Output is correct |
93 |
Correct |
217 ms |
196636 KB |
Output is correct |
94 |
Correct |
232 ms |
196620 KB |
Output is correct |
95 |
Correct |
207 ms |
196612 KB |
Output is correct |
96 |
Correct |
215 ms |
196544 KB |
Output is correct |
97 |
Correct |
215 ms |
196624 KB |
Output is correct |
98 |
Correct |
374 ms |
196616 KB |
Output is correct |
99 |
Correct |
373 ms |
196612 KB |
Output is correct |
100 |
Correct |
351 ms |
196608 KB |
Output is correct |