```
public class Solution {
public int candy(int[] ratings) {
int sum = 0;
int pre = 0;
int t = 0;
int n = 0;
while(t<ratings.length){
if(t==ratings.length-1){
sum+=2;
continue;
}else if(ratings[t]>ratings[t+1]){
while(ratings[t]>ratings[t+1]&&t<ratings.length-1){
if(t==ratings.length-2){
n++;
}
t++;
n++;
}
sum+=(1+n)*n/2;
pre=1;
n=0;
}else{
pre+=1;
sum+=pre;
t++;
}
}
return sum;
}
```

}