```
class Solution {
public:
int candy(vector<int>& ratings) {
int last = 1, cost = 0;
for(int i = 0;i < ratings.size();i++){
if(i+1 < ratings.size() && ratings[i] > ratings[i+1]){
int c = 0;
while(i+1 < ratings.size() && ratings[i] > ratings[i+1])
c++,i++;
last = (last <= c) ? c+1 : last;
cost+= last+(c*(c+1))/2;
last = (i+1 < ratings.size() && ratings[i] < ratings[i+1]) ? 2 : 1;
}else{
cost+=(last);
if(i+1 < ratings.size()&& ratings[i+1] > ratings[i])
last++;
else
last = 1;
}
}
return cost;
}
};
```

```
Testcases :
Input :
[0]
[0,1,2,3,2,1]
[0,1,2,5,3,2,7]
[5,3,7,3]
[1,2,3]
[1,2,3,5,4,3,2,1]
[1,2,3,5,4,3,2,1,4,3,2,1]
[1,2,3,5,4,3,2,1,4,3,2,1,3,2,1,1,2,3,4]
[1,2,3,5,4,3,2,1,4,3,2,1,3,2,1,1,2,3,4,4,3,2,1]
[10,10,10,10,10,10]
[5,10,10,10,10,10,10]
Output :
1
13
15
6
6
21
31
47
57
6
8
```