def candy(ratings):
    n = len(ratings)
    if n == 0:
        return 0
    
    # Step 1: Initialize candies array
    candies = [1] * n
    
    # Step 2: Left to right pass
    for i in range(1, n):
        if ratings[i] > ratings[i - 1]:
            candies[i] = candies[i - 1] + 1
    
    # Step 3: Right to left pass
    for i in range(n - 2, -1, -1):
        if ratings[i] > ratings[i + 1]:
            candies[i] = max(candies[i], candies[i + 1] + 1)
    
    # Step 4: Calculate total candies
    return sum(candies)

# Example usage:
print(candy([1, 0, 2]))  # Output: 5
print(candy([1, 2, 2]))  # Output: 4