import
java.util.HashMap;
import
java.util.Map;
public
class
Main {
static
Map<Tuple, Integer> dp =
new
HashMap<>();
static
int
maxPreSum(
int
[] a,
int
[] b,
int
x,
int
y)
{
if
(dp.containsKey(
new
Tuple(x, y))) {
return
dp.get(
new
Tuple(x, y));
}
if
(x == a.length && y == b.length) {
return
0
;
}
int
curr =
0
;
if
(dp.containsKey(
new
Tuple(x, y))) {
curr = dp.get(
new
Tuple(x, y));
}
if
(x == a.length) {
curr = Math.max(
curr, b[y] + maxPreSum(a, b, x, y +
1
));
}
else
if
(y == b.length) {
curr = Math.max(
curr, a[x] + maxPreSum(a, b, x +
1
, y));
}
else
{
int
max = Math.max(
a[x] + maxPreSum(a, b, x +
1
, y),
b[y] + maxPreSum(a, b, x, y +
1
));
curr = Math.max(curr, max);
}
dp.put(
new
Tuple(x, y), curr);
return
dp.get(
new
Tuple(x, y));
}
public
static
void
main(String[] args)
{
int
[] A = {
2
,
1
,
13
,
5
,
14
};
int
[] B = { -
1
,
4
, -
13
};
System.out.println(maxPreSum(A, B,
0
,
0
));
}
static
class
Tuple {
int
x;
int
y;
public
Tuple(
int
x,
int
y)
{
this
.x = x;
this
.y = y;
}
@Override
public
int
hashCode()
{
final
int
prime =
31
;
int
result =
1
;
result = prime * result + x;
result = prime * result + y;
return
result;
}
@Override
public
boolean
equals(Object obj)
{
if
(
this
== obj) {
return
true
;
}
if
(obj ==
null
) {
return
false
;
}
if
(getClass() != obj.getClass()) {
return
false
;
}
Tuple other = (Tuple)obj;
if
(x != other.x) {
return
false
;
}
if
(y != other.y) {
return
false
;
}
return
true
;
}
}
}