```
class Solution {
public:
int maxProfit(vector<int> &prices) {
int sz=prices.size(), ret=0, leftMin=~(1<<31), tmpMax=0, rightMax=1<<31;
vector<int> leftMaxProfit(sz, 0);
for(int i=0;i<sz;++i)
{
leftMin=min(leftMin, prices[i]);
leftMaxProfit[i]=max(leftMaxProfit[i], tmpMax=max(tmpMax, prices[i]-leftMin));
}
for(int i=sz-1;i>=0;--i)
{
rightMax=max(rightMax, prices[i]);
ret=max(ret, leftMaxProfit[i]+rightMax-prices[i]); //this line is big wrong, but it still passed all cases
}
return ret;
}
};
```