import
java.util.*;
class
GFG{
static
int
solve(
int
a[],
int
b[],
int
i,
int
j,
int
sum,
HashMap<String, Integer> mp)
{
if
(sum ==
0
)
return
0
;
if
(sum <
0
)
return
Integer.MIN_VALUE;
if
(i ==
0
|| j ==
0
) {
if
(sum ==
0
)
return
0
;
else
return
Integer.MIN_VALUE;
}
String temp = String.valueOf(i) +
'#'
+ String.valueOf(j) +
'#'
+ String.valueOf(sum);
if
(mp.containsKey(temp))
return
mp.get(temp);
if
(a[i -
1
] == b[j -
1
]) {
mp.put(temp, Math.max(
1
+ solve(a, b, i -
1
, j -
1
,
sum - a[i -
1
], mp),
solve(a, b, i -
1
, j -
1
, sum, mp)));
return
mp.get(temp);
}
mp.put(temp, Math.max(solve(a, b, i -
1
, j, sum, mp),
solve(a, b, i, j -
1
, sum, mp)));
return
mp.get(temp);
}
public
static
void
main(String[] args)
{
int
a[] = {
9
,
11
,
2
,
1
,
6
,
2
,
7
};
int
b[] = {
1
,
2
,
6
,
9
,
2
,
3
,
11
,
7
};
HashMap<String, Integer> mp =
new
HashMap<>();
int
n = a.length;
int
m = b.length;
int
sum =
18
;
int
ans = solve(a, b, n, m, sum, mp);
if
(ans >=
0
)
System.out.print(ans +
"\n"
);
else
System.out.print(-
1
);
}
}