PS/백준

주식 - 11501

Mingi Kim 2025. 2. 13. 09:55

문제

https://www.acmicpc.net/problem/11501

느낀점

dp, 주식과 같은 문제를 풀 때, 뒤에서 생각해보는 것도 방법이다

풀이

#include <iostream>
#include <vector>

int main()
{
    int T, N, price, base = 0;
    bool asc;
    std::vector<int> priceList;
    std::cin >> T;
    for (int i = 0; i < T; i++)
    {
        long answer = 0;
        int maxValue = 0;
        priceList.clear();
        std::cin >> N;

        for (int j = 0; j < N; j++)
        {
            std::cin >> price;
            priceList.push_back(price);
        }

        for (int k = N - 1; k >= 0; k--)
        {
            if (priceList[k] >= maxValue)
            {
                maxValue = priceList[k];
            }
            else
            {
                answer += (maxValue - priceList[k]);
            }
        }
        std::cout << answer << "\n";
    }
    return 0;
}