public int candy(int[] ratings) {
			if (ratings.length == 0)
				return 0;

			if (ratings.length == 1)
				return 1;

			int sum = 0;
			int lastLeast = 0;
			
			for (int i = 0; i < ratings.length;) {
				int j = i;
				for (; j < ratings.length - 1; j++) {
					if (ratings[j] <= ratings[j + 1]) {
						break;
					}
				}
				int count = j - i + 1;

			int blockFirstCandyMin;
				if (i - 1 < 0 || ratings[i] == ratings[i - 1]) {
					blockFirstCandyMin = 1;
				} else {
					blockFirstCandyMin = lastLeast +1;
				}

				if (count >= blockFirstCandyMin) {
					sum += (count + 1) * count / 2;
				} else {
					sum = sum + blockFirstCandyMin;
					sum += (count) * (count - 1) / 2;
				}

				if(count == 1){
					lastLeast = blockFirstCandyMin;
				}else{
					lastLeast = 1;
				}
				
				i = i + count;
			}
			return sum;
		}