```
/*
Finding local minima(buying rate) and local maxima(selling rate) and then find subsequent profits.
*/
class Solution {
public:
int maxProfit(vector<int>& prices) {
int n= prices.size();
int profit=0;
for(int i=0; i<n; i++){
while(i< n-1 && prices[i]> prices[i+1]) i++;
profit-= prices[i];
while(i< n-1 && prices[i] < prices[i+1]) i++;
profit += prices[i];
}
return profit;
}
};
```