class Solution

{

public int maxProfit(int[] prices, int fee)

{

int n = prices.length;

if(n == 0) return 0;

int min = Integer.MAX_VALUE;

int count = 0;

boolean isBuy = false;

int tmp = 0;

for(int i = 0; i < n; i++)

{

if(!isBuy && prices[i] < min)

{

min = prices[i];

}

else if(!isBuy && prices[i] > min + fee)

{

tmp = prices[i];

isBuy = true;

}

else if(isBuy && prices[i] < tmp - fee)

{

isBuy = false;

count += tmp - min - fee;

min = prices[i];

}

else if(isBuy && prices[i] > tmp)

{

tmp = prices[i];

}

}

if(isBuy)

{

count += tmp - min - fee;

}

return count;

}

}