#include<bits/stdc++.h>
#define ll long long
#define f first
#define s second
#define pb push_back
#define inf 1000000000000
using namespace std;
ll k,n,m,A,B,v[5005],s[5005],t[5005];
ll dp[5005][5005],mx[5005];
int main(){
ios::sync_with_stdio(false);
cin >> k >> n >> m >> A >> B;
A = -A;
B = -B;
for(int i=1; i<=k; i++)
cin >> v[i];
for(int i=1; i<=n; i++)
cin >> s[i];
for(int i=1; i<=m; i++)
cin >> t[i];
for(int i=0; i<=n; i++)
for(int j=0; j<=m; j++)
dp[i][j] = -inf;
for(int i=0; i<=m; i++)
mx[i] = -inf;
for(int i=1; i<=m; i++){
if(s[1] == t[i])
if(i == 1)dp[1][i] = v[s[1]];
else dp[1][i] = v[s[1]] - A - (i - 1) * B;
}
for(int i=2; i<=n; i++){
ll cur = -inf;
for(int j=1; j<=m; j++){
dp[i][j] = -inf;
if(s[i] != t[j]){
cur = max(cur - B , max(mx[j - 1] , dp[i - 1][j - 1]) - A - B);
continue;
}
if(j == 1){
dp[i][j] = v[t[1]];
}
else {
dp[i][j] = v[s[i]] - A - (j - 1) * B;
}
dp[i][j] = max(dp[i][j] , max(dp[i - 1][j - 1] , cur) + v[s[i]]);
dp[i][j] = max(dp[i][j] , mx[j - 1] + v[s[i]]);
cur = max(cur - B , max(mx[j - 1] , dp[i - 1][j - 1]) - A - B);
}
for(int j=1; j<=m; j++){
mx[j] = max(mx[j] - B , dp[i - 1][j] - A - B);
}
}
ll ans = -A - m * B;
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++){
if(j == m)
ans = max(ans , dp[i][j]);
else ans = max(ans , dp[i][j] - A - (m - j) * B);
}
cout << ans << '\n';
return 0;
}
Compilation message
VisitingSingapore.cpp: In function 'int main()':
VisitingSingapore.cpp:28:11: warning: suggest explicit braces to avoid ambiguous 'else' [-Wdangling-else]
28 | if(s[1] == t[i])
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1408 KB |
Output is correct |
2 |
Correct |
13 ms |
10880 KB |
Output is correct |
3 |
Correct |
1 ms |
1408 KB |
Output is correct |
4 |
Correct |
2 ms |
3968 KB |
Output is correct |
5 |
Correct |
1 ms |
1920 KB |
Output is correct |
6 |
Correct |
5 ms |
5376 KB |
Output is correct |
7 |
Correct |
1 ms |
1152 KB |
Output is correct |
8 |
Correct |
2 ms |
2688 KB |
Output is correct |
9 |
Correct |
8 ms |
7296 KB |
Output is correct |
10 |
Correct |
15 ms |
12288 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
3456 KB |
Output is correct |
4 |
Correct |
1 ms |
768 KB |
Output is correct |
5 |
Correct |
2 ms |
1536 KB |
Output is correct |
6 |
Correct |
8 ms |
6528 KB |
Output is correct |
7 |
Correct |
7 ms |
6144 KB |
Output is correct |
8 |
Correct |
11 ms |
9088 KB |
Output is correct |
9 |
Correct |
3 ms |
1920 KB |
Output is correct |
10 |
Correct |
15 ms |
12288 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
30080 KB |
Output is correct |
2 |
Correct |
16 ms |
18176 KB |
Output is correct |
3 |
Correct |
95 ms |
79992 KB |
Output is correct |
4 |
Correct |
201 ms |
174456 KB |
Output is correct |
5 |
Correct |
64 ms |
51448 KB |
Output is correct |
6 |
Correct |
88 ms |
76408 KB |
Output is correct |
7 |
Correct |
163 ms |
143608 KB |
Output is correct |
8 |
Correct |
59 ms |
54912 KB |
Output is correct |
9 |
Correct |
104 ms |
91512 KB |
Output is correct |
10 |
Correct |
238 ms |
196220 KB |
Output is correct |
11 |
Correct |
238 ms |
195708 KB |
Output is correct |
12 |
Correct |
239 ms |
196216 KB |
Output is correct |
13 |
Correct |
238 ms |
196088 KB |
Output is correct |
14 |
Correct |
321 ms |
196220 KB |
Output is correct |
15 |
Correct |
319 ms |
196188 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
30080 KB |
Output is correct |
2 |
Correct |
16 ms |
18176 KB |
Output is correct |
3 |
Correct |
95 ms |
79992 KB |
Output is correct |
4 |
Correct |
201 ms |
174456 KB |
Output is correct |
5 |
Correct |
64 ms |
51448 KB |
Output is correct |
6 |
Correct |
88 ms |
76408 KB |
Output is correct |
7 |
Correct |
163 ms |
143608 KB |
Output is correct |
8 |
Correct |
59 ms |
54912 KB |
Output is correct |
9 |
Correct |
104 ms |
91512 KB |
Output is correct |
10 |
Correct |
238 ms |
196220 KB |
Output is correct |
11 |
Correct |
238 ms |
195708 KB |
Output is correct |
12 |
Correct |
239 ms |
196216 KB |
Output is correct |
13 |
Correct |
238 ms |
196088 KB |
Output is correct |
14 |
Correct |
321 ms |
196220 KB |
Output is correct |
15 |
Correct |
319 ms |
196188 KB |
Output is correct |
16 |
Correct |
133 ms |
117880 KB |
Output is correct |
17 |
Correct |
163 ms |
142712 KB |
Output is correct |
18 |
Correct |
170 ms |
129272 KB |
Output is correct |
19 |
Correct |
32 ms |
26880 KB |
Output is correct |
20 |
Correct |
107 ms |
93208 KB |
Output is correct |
21 |
Correct |
11 ms |
9984 KB |
Output is correct |
22 |
Correct |
35 ms |
28928 KB |
Output is correct |
23 |
Correct |
67 ms |
57088 KB |
Output is correct |
24 |
Correct |
31 ms |
29124 KB |
Output is correct |
25 |
Correct |
240 ms |
195792 KB |
Output is correct |
26 |
Correct |
266 ms |
195704 KB |
Output is correct |
27 |
Correct |
239 ms |
196344 KB |
Output is correct |
28 |
Correct |
239 ms |
196344 KB |
Output is correct |
29 |
Correct |
294 ms |
195704 KB |
Output is correct |
30 |
Correct |
291 ms |
195960 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
30080 KB |
Output is correct |
2 |
Correct |
16 ms |
18176 KB |
Output is correct |
3 |
Correct |
95 ms |
79992 KB |
Output is correct |
4 |
Correct |
201 ms |
174456 KB |
Output is correct |
5 |
Correct |
64 ms |
51448 KB |
Output is correct |
6 |
Correct |
88 ms |
76408 KB |
Output is correct |
7 |
Correct |
163 ms |
143608 KB |
Output is correct |
8 |
Correct |
59 ms |
54912 KB |
Output is correct |
9 |
Correct |
104 ms |
91512 KB |
Output is correct |
10 |
Correct |
238 ms |
196220 KB |
Output is correct |
11 |
Correct |
238 ms |
195708 KB |
Output is correct |
12 |
Correct |
239 ms |
196216 KB |
Output is correct |
13 |
Correct |
238 ms |
196088 KB |
Output is correct |
14 |
Correct |
321 ms |
196220 KB |
Output is correct |
15 |
Correct |
319 ms |
196188 KB |
Output is correct |
16 |
Correct |
210 ms |
182520 KB |
Output is correct |
17 |
Correct |
109 ms |
92920 KB |
Output is correct |
18 |
Correct |
33 ms |
28408 KB |
Output is correct |
19 |
Correct |
11 ms |
10752 KB |
Output is correct |
20 |
Correct |
5 ms |
4224 KB |
Output is correct |
21 |
Correct |
7 ms |
5248 KB |
Output is correct |
22 |
Correct |
100 ms |
86264 KB |
Output is correct |
23 |
Correct |
26 ms |
21632 KB |
Output is correct |
24 |
Correct |
177 ms |
152184 KB |
Output is correct |
25 |
Correct |
237 ms |
195832 KB |
Output is correct |
26 |
Correct |
245 ms |
195836 KB |
Output is correct |
27 |
Correct |
243 ms |
195704 KB |
Output is correct |
28 |
Correct |
237 ms |
195960 KB |
Output is correct |
29 |
Correct |
293 ms |
195832 KB |
Output is correct |
30 |
Correct |
291 ms |
196472 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
640 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
640 KB |
Output is correct |
5 |
Correct |
1 ms |
512 KB |
Output is correct |
6 |
Correct |
1 ms |
768 KB |
Output is correct |
7 |
Correct |
1 ms |
512 KB |
Output is correct |
8 |
Correct |
1 ms |
512 KB |
Output is correct |
9 |
Correct |
1 ms |
640 KB |
Output is correct |
10 |
Correct |
1 ms |
768 KB |
Output is correct |
11 |
Correct |
1 ms |
896 KB |
Output is correct |
12 |
Correct |
1 ms |
768 KB |
Output is correct |
13 |
Correct |
1 ms |
768 KB |
Output is correct |
14 |
Correct |
1 ms |
768 KB |
Output is correct |
15 |
Correct |
1 ms |
768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1408 KB |
Output is correct |
2 |
Correct |
13 ms |
10880 KB |
Output is correct |
3 |
Correct |
1 ms |
1408 KB |
Output is correct |
4 |
Correct |
2 ms |
3968 KB |
Output is correct |
5 |
Correct |
1 ms |
1920 KB |
Output is correct |
6 |
Correct |
5 ms |
5376 KB |
Output is correct |
7 |
Correct |
1 ms |
1152 KB |
Output is correct |
8 |
Correct |
2 ms |
2688 KB |
Output is correct |
9 |
Correct |
8 ms |
7296 KB |
Output is correct |
10 |
Correct |
15 ms |
12288 KB |
Output is correct |
11 |
Correct |
1 ms |
512 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
4 ms |
3456 KB |
Output is correct |
14 |
Correct |
1 ms |
768 KB |
Output is correct |
15 |
Correct |
2 ms |
1536 KB |
Output is correct |
16 |
Correct |
8 ms |
6528 KB |
Output is correct |
17 |
Correct |
7 ms |
6144 KB |
Output is correct |
18 |
Correct |
11 ms |
9088 KB |
Output is correct |
19 |
Correct |
3 ms |
1920 KB |
Output is correct |
20 |
Correct |
15 ms |
12288 KB |
Output is correct |
21 |
Correct |
31 ms |
30080 KB |
Output is correct |
22 |
Correct |
16 ms |
18176 KB |
Output is correct |
23 |
Correct |
95 ms |
79992 KB |
Output is correct |
24 |
Correct |
201 ms |
174456 KB |
Output is correct |
25 |
Correct |
64 ms |
51448 KB |
Output is correct |
26 |
Correct |
88 ms |
76408 KB |
Output is correct |
27 |
Correct |
163 ms |
143608 KB |
Output is correct |
28 |
Correct |
59 ms |
54912 KB |
Output is correct |
29 |
Correct |
104 ms |
91512 KB |
Output is correct |
30 |
Correct |
238 ms |
196220 KB |
Output is correct |
31 |
Correct |
238 ms |
195708 KB |
Output is correct |
32 |
Correct |
239 ms |
196216 KB |
Output is correct |
33 |
Correct |
238 ms |
196088 KB |
Output is correct |
34 |
Correct |
321 ms |
196220 KB |
Output is correct |
35 |
Correct |
319 ms |
196188 KB |
Output is correct |
36 |
Correct |
133 ms |
117880 KB |
Output is correct |
37 |
Correct |
163 ms |
142712 KB |
Output is correct |
38 |
Correct |
170 ms |
129272 KB |
Output is correct |
39 |
Correct |
32 ms |
26880 KB |
Output is correct |
40 |
Correct |
107 ms |
93208 KB |
Output is correct |
41 |
Correct |
11 ms |
9984 KB |
Output is correct |
42 |
Correct |
35 ms |
28928 KB |
Output is correct |
43 |
Correct |
67 ms |
57088 KB |
Output is correct |
44 |
Correct |
31 ms |
29124 KB |
Output is correct |
45 |
Correct |
240 ms |
195792 KB |
Output is correct |
46 |
Correct |
266 ms |
195704 KB |
Output is correct |
47 |
Correct |
239 ms |
196344 KB |
Output is correct |
48 |
Correct |
239 ms |
196344 KB |
Output is correct |
49 |
Correct |
294 ms |
195704 KB |
Output is correct |
50 |
Correct |
291 ms |
195960 KB |
Output is correct |
51 |
Correct |
210 ms |
182520 KB |
Output is correct |
52 |
Correct |
109 ms |
92920 KB |
Output is correct |
53 |
Correct |
33 ms |
28408 KB |
Output is correct |
54 |
Correct |
11 ms |
10752 KB |
Output is correct |
55 |
Correct |
5 ms |
4224 KB |
Output is correct |
56 |
Correct |
7 ms |
5248 KB |
Output is correct |
57 |
Correct |
100 ms |
86264 KB |
Output is correct |
58 |
Correct |
26 ms |
21632 KB |
Output is correct |
59 |
Correct |
177 ms |
152184 KB |
Output is correct |
60 |
Correct |
237 ms |
195832 KB |
Output is correct |
61 |
Correct |
245 ms |
195836 KB |
Output is correct |
62 |
Correct |
243 ms |
195704 KB |
Output is correct |
63 |
Correct |
237 ms |
195960 KB |
Output is correct |
64 |
Correct |
293 ms |
195832 KB |
Output is correct |
65 |
Correct |
291 ms |
196472 KB |
Output is correct |
66 |
Correct |
1 ms |
640 KB |
Output is correct |
67 |
Correct |
1 ms |
384 KB |
Output is correct |
68 |
Correct |
1 ms |
384 KB |
Output is correct |
69 |
Correct |
1 ms |
640 KB |
Output is correct |
70 |
Correct |
1 ms |
512 KB |
Output is correct |
71 |
Correct |
1 ms |
768 KB |
Output is correct |
72 |
Correct |
1 ms |
512 KB |
Output is correct |
73 |
Correct |
1 ms |
512 KB |
Output is correct |
74 |
Correct |
1 ms |
640 KB |
Output is correct |
75 |
Correct |
1 ms |
768 KB |
Output is correct |
76 |
Correct |
1 ms |
896 KB |
Output is correct |
77 |
Correct |
1 ms |
768 KB |
Output is correct |
78 |
Correct |
1 ms |
768 KB |
Output is correct |
79 |
Correct |
1 ms |
768 KB |
Output is correct |
80 |
Correct |
1 ms |
768 KB |
Output is correct |
81 |
Correct |
201 ms |
176120 KB |
Output is correct |
82 |
Correct |
55 ms |
48248 KB |
Output is correct |
83 |
Correct |
94 ms |
79264 KB |
Output is correct |
84 |
Correct |
50 ms |
42616 KB |
Output is correct |
85 |
Correct |
85 ms |
73720 KB |
Output is correct |
86 |
Correct |
75 ms |
66680 KB |
Output is correct |
87 |
Correct |
15 ms |
12800 KB |
Output is correct |
88 |
Correct |
10 ms |
10624 KB |
Output is correct |
89 |
Correct |
32 ms |
28800 KB |
Output is correct |
90 |
Correct |
268 ms |
196112 KB |
Output is correct |
91 |
Correct |
240 ms |
196088 KB |
Output is correct |
92 |
Correct |
239 ms |
196036 KB |
Output is correct |
93 |
Correct |
242 ms |
196096 KB |
Output is correct |
94 |
Correct |
243 ms |
196088 KB |
Output is correct |
95 |
Correct |
236 ms |
196088 KB |
Output is correct |
96 |
Correct |
257 ms |
196176 KB |
Output is correct |
97 |
Correct |
240 ms |
196472 KB |
Output is correct |
98 |
Correct |
289 ms |
195832 KB |
Output is correct |
99 |
Correct |
289 ms |
196088 KB |
Output is correct |
100 |
Correct |
295 ms |
195576 KB |
Output is correct |