Theoretically, we just need to sum over all forward differences which are positive. But, in this question, we are not allowed to sell the share continuously, because only when we hold a share can you sell a share. So, the correct solution is dynamic programming(DP).

```
int maxProfit(vector<int>& prices) {
int n = prices.size();
if (n <= 1) return 0;
vector<int> dp(n,0);
int minPrice = prices[0],res = 0;
for (int i = 1;i < n;++i) {
minPrice = min(minPrice,prices[i]);
if (prices[i]-minPrice>=dp[i-1]) dp[i] = prices[i]-minPrice;
else {
minPrice = prices[i];
res += dp[i-1];
}
}
return res+dp[n-1];
}
```