/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
{
// your code goes here
}
public int maxProfit(int[] prices, int fee) {
int[] hold = new int[prices.length];
int[] free = new int[prices.length];
hold[0] = 0 - prices[0];
free[0] = 0;
for (int i = 1 ; i < prices.length ; i++) {
free
[i
] = Math.
max(free
[i
- 1], hold
[i
- 1] + prices
[i
] - fee
); hold
[i
] = Math.
max(hold
[i
- 1], free
[i
- 1] - prices
[i
]); }
return free[prices.length - 1];
}
}
LyogcGFja2FnZSB3aGF0ZXZlcjsgLy8gZG9uJ3QgcGxhY2UgcGFja2FnZSBuYW1lISAqLwoKaW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS5sYW5nLio7CmltcG9ydCBqYXZhLmlvLio7CgovKiBOYW1lIG9mIHRoZSBjbGFzcyBoYXMgdG8gYmUgIk1haW4iIG9ubHkgaWYgdGhlIGNsYXNzIGlzIHB1YmxpYy4gKi8KY2xhc3MgSWRlb25lCnsKCXB1YmxpYyBzdGF0aWMgdm9pZCBtYWluIChTdHJpbmdbXSBhcmdzKSB0aHJvd3MgamF2YS5sYW5nLkV4Y2VwdGlvbgoJewoJCS8vIHlvdXIgY29kZSBnb2VzIGhlcmUKCX0KICAgIHB1YmxpYyBpbnQgbWF4UHJvZml0KGludFtdIHByaWNlcywgaW50IGZlZSkgewogICAgICAgIGludFtdIGhvbGQgPSBuZXcgaW50W3ByaWNlcy5sZW5ndGhdOwogICAgICAgIGludFtdIGZyZWUgPSBuZXcgaW50W3ByaWNlcy5sZW5ndGhdOwoKICAgICAgICBob2xkWzBdID0gMCAtIHByaWNlc1swXTsKICAgICAgICBmcmVlWzBdID0gMDsKCiAgICAgICAgZm9yIChpbnQgaSA9IDEgOyBpIDwgcHJpY2VzLmxlbmd0aCA7IGkrKykgewogICAgICAgICAgICBmcmVlW2ldID0gTWF0aC5tYXgoZnJlZVtpIC0gMV0sIGhvbGRbaSAtIDFdICsgcHJpY2VzW2ldIC0gZmVlKTsKICAgICAgICAgICAgaG9sZFtpXSA9IE1hdGgubWF4KGhvbGRbaSAtIDFdLCBmcmVlW2kgLSAxXSAtIHByaWNlc1tpXSk7CiAgICAgICAgfQogICAgICAgIHJldHVybiBmcmVlW3ByaWNlcy5sZW5ndGggLSAxXTsKICAgIH0KCn0=