import
java.util.*;
public
class
Main {
public
static
int
f(
int
idx,
int
n,
int
buy,
int
[] prices,
List<List<Integer>> dp,
int
fee) {
if
(idx == n) {
return
0
;
}
if
(dp.get(idx).get(buy) != -
1
) {
return
dp.get(idx).get(buy);
}
int
profit =
0
;
if
(buy ==
0
) {
dp.get(idx).set(buy, profit
= Math.max(-prices[idx]
+ f(idx +
1
, n,
1
, prices, dp, fee),
f(idx +
1
, n,
0
, prices, dp, fee)));
}
else
{
dp.get(idx).set(buy, profit
= Math.max(prices[idx] - fee
+ f(idx +
1
, n,
0
, prices, dp, fee),
f(idx +
1
, n,
1
, prices, dp, fee)));
}
return
profit;
}
public
static
int
MaxProfit(
int
[] prices,
int
n,
int
fee) {
List<List<Integer>> dp =
new
ArrayList<>();
for
(
int
i =
0
; i < n; i++) {
dp.add(
new
ArrayList<>(Arrays.asList(-
1
, -
1
)));
}
return
f(
0
, n,
0
, prices, dp, fee);
}
public
static
void
main(String[] args) {
int
[] arr = {
6
,
1
,
7
,
2
,
8
,
4
};
int
n = arr.length;
int
transactionFee =
2
;
System.out.println(MaxProfit(arr, n, transactionFee));
}
}