class Solution { public int candy(int[] ratings) { int[] candies=new int[ratings.length]; for(int i=0;i<ratings.length;i++) candies[i]=(i!=0&&ratings[i-1]<ratings[i])?candies[i-1]+1:1; for(int i=ratings.length-2;i>=0;i--) if(ratings[i]>ratings[i+1]) candies[i]=Math.max(candies[i],candies[i+1]+1); int res=0; for(int candy:candies) res+=candy; return res; } }