class Solution {
  public:
    /**
     *
     * @param ratings int整型vector
     * @return int整型
     */
    int candy(vector<int>& ratings) {
        // write code here
        int n = ratings.size();
        if (n == 0) return 0;

        std::vector<int> candies(n, 1);

        // 从左到右
        for (int i = 1; i < n; ++i) {
            if (ratings[i] > ratings[i - 1]) {
                candies[i] = candies[i - 1] + 1;
            }
        }

        // 从右到左
        for (int i = n - 2; i >= 0; --i) {
            if (ratings[i] > ratings[i + 1]) {
                candies[i] = std::max(candies[i], candies[i + 1] + 1);
            }
        }

        // 计算总糖果
        int totalCandies = 0;
        for (int candy : candies) {
            totalCandies += candy;
        }

        return totalCandies;
    }
};