import
java.util.*;
public
class
Main {
static
int
N, X, Y;
static
List<Map<Integer, Map<Integer, Integer>>> mem =
new
ArrayList<>();
static
List<Map<Integer, Map<Integer, Boolean>>> vis =
new
ArrayList<>();
static
List<Integer> A_right_sum =
new
ArrayList<>();
static
List<Integer> B_right_sum =
new
ArrayList<>();
public
static
boolean
get_vis_val(
int
i,
int
x,
int
y) {
if
(i == N)
return
true
;
return
vis.get(i).get(x).get(y);
}
public
static
int
get_mem_val(
int
i,
int
x,
int
y) {
if
(i == N)
return
0
;
return
mem.get(i).get(x).get(y);
}
public
static
void
find_ans(
int
i,
int
x,
int
y, List<Integer> A, List<Integer> B) {
if
(get_vis_val(i, x, y))
return
;
vis.get(i).get(x).put(y,
true
);
if
(x ==
0
) {
mem.get(i).get(x).put(y, B_right_sum.get(i));
}
else
if
(y ==
0
) {
mem.get(i).get(x).put(y, A_right_sum.get(i));
}
else
{
find_ans(i +
1
, x -
1
, y, A, B);
find_ans(i +
1
, x, y -
1
, A, B);
mem.get(i).get(x).put(y,
Math.max(get_mem_val(i +
1
, x -
1
, y) + A.get(i),
get_mem_val(i +
1
, x, y -
1
) + B.get(i)));
}
}
public
static
void
main(String[] args) {
int
[] a = {
1
,
2
,
3
,
4
,
5
};
int
[] b = {
5
,
4
,
3
,
2
,
1
};
N = a.length;
X =
3
;
Y =
3
;
List<Integer> A =
new
ArrayList<>();
List<Integer> B =
new
ArrayList<>();
for
(
int
i =
0
; i < N; i++) {
A.add(a[i]);
B.add(b[i]);
}
for
(
int
i =
0
; i <= N; i++) {
mem.add(
new
HashMap<>());
vis.add(
new
HashMap<>());
for
(
int
j =
0
; j <= X; j++) {
mem.get(i).put(j,
new
HashMap<>());
vis.get(i).put(j,
new
HashMap<>());
for
(
int
k =
0
; k <= Y; k++) {
mem.get(i).get(j).put(k,
0
);
vis.get(i).get(j).put(k,
false
);
}
}
}
A_right_sum.add(A.get(N -
1
));
B_right_sum.add(B.get(N -
1
));
for
(
int
i = N -
2
; i >=
0
; i--) {
A_right_sum.add(
0
, A_right_sum.get(
0
) + A.get(i));
B_right_sum.add(
0
, B_right_sum.get(
0
) + B.get(i));
}
find_ans(
0
, X, Y, A, B);
System.out.println(get_mem_val(
0
, X, Y));
}
}