Code:
#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 3003
#define ll long long
#define setIO(s) freopen(s".in", "r" , stdin)
using namespace std;
char str[N];
int S[N], T[N], d[6][6], n, m;
ll f[N][N], g[N][N], h[N][N];
int id(char c)
{
if(c == 'A') return 1;
if(c == 'T') return 2;
if(c == 'G') return 3;
if(c == 'C') return 4;
}
int main()
{
// setIO("input");
int i , j, A, B;
scanf("%s", str + 1), n = strlen(str + 1);
for(i = 1; i <= n ; ++ i) S[i] = id(str[i]);
scanf("%s", str + 1), m = strlen(str + 1);
for(i = 1; i <= m ; ++ i) T[i] = id(str[i]);
for(i = 1; i <= 4 ; ++ i)
{
for(j = 1; j <= 4; ++j) scanf("%d", &d[i][j]);
}
scanf("%d%d", &A, &B);
memset(f, -63, sizeof(f)), memset(g, -63, sizeof(g)), memset(h, -63, sizeof(h));
g[1][0] = h[0][1] = - A, f[0][0] = 0;
for(i = 1; i <= n ; ++ i)
{
for(j = 1; j <= m ; ++ j)
{
f[i][j] = max(f[i - 1][j - 1], max(g[i - 1][j - 1], h[i - 1][j - 1])) + d[S[i]][T[j]];
g[i][j] = max(max(f[i - 1][j], h[i - 1][j]) - A, g[i - 1][j] - B);
h[i][j] = max(max(f[i][j - 1], g[i][j - 1]) - A, h[i][j - 1] - B);
}
}
printf("%lld\n", max(max(f[n][m], g[n][m]), h[n][m]));
return 0;
}