Best Time to Buy and Sell Stock II

this is leetcode problem, my logic is correct, but my code is not able to make all test pass.
pls help me in this:

int maxProfit(vector& prices) {
unordered_map<int , int> m;
m[prices[0]]=0;
for(int i=1; i<prices.size(); i++){
m[prices[i]]=prices[i] - prices[i-1];
}
int ans=0;
for(auto it= m.begin(); it!= m.end() ; it++){
if((*it).second >0){
ans = ans + (*it).second;
}
}
return ans;
}
};