14719번

seuls2·2023년 8월 5일
0

BOJ

목록 보기
49/55
post-thumbnail

14719

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int h, w;
vector<int> heights;

int getAnswer()
{
    int sum = 0;
    for (int i = 1; i < w - 1; i++)
    {
        int maxLeftIndex = i - 1;
        int maxRightIndex = i + 1;
        for (int leftIndex = i - 1; leftIndex >= 0; leftIndex--)
        {
            maxLeftIndex = heights[leftIndex] < heights[maxLeftIndex] ? maxLeftIndex : leftIndex;
        }

        for (int rightIndex = i + 1; rightIndex < w; rightIndex++)
        {
            maxRightIndex = heights[rightIndex] < heights[maxRightIndex] ? maxRightIndex : rightIndex;
        }
        int minHeight = min(heights[maxLeftIndex], heights[maxRightIndex]);
        if (heights[i] < minHeight)
        {
            sum += minHeight - heights[i];
        }
    }
    return sum;
}

int main()
{
    cin >> h >> w;

    for (int i = 0; i < w; i++)
    {
        int num;
        cin >> num;
        heights.push_back(num);
    }

    cout << getAnswer();
}
profile
공부 기록용 ( ᵕ·̮ᵕ )♩

0개의 댓글