```
int candy(vector<int> &ratings) {
if(ratings.size() == 0) return 0;
vector<int> candies;
candies.push_back(0);
for (int i = 1; i < ratings.size(); i++)
{
if(ratings[i] > ratings[i-1])
candies.push_back(candies[i-1] + 1);
else if(ratings[i] < ratings[i-1])
candies.push_back(candies[i-1] - 1);
else
candies.push_back(candies[i-1]);
}
int min=0;
for(int i=0; i<candies.size(); i++)
if(candies[i]<min)
min = candies[i];
int res;
for(int i=0; i<candies.size(); i++)
res += candies[i] + (1-min);
return res;
}
```

Thanks!