알고리즘/그리디

[백준/그리디/C++] 115001번 주식 *

데메즈 2023. 3. 6. 18:46
728x90
반응형

문제는 여기!

#include <bits/stdc++.h>

using namespace std;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int stock_value[1000000];
    int test_num, day_num;
    cin >> test_num;

    long long profit_sum;
    int max_stock;

    for(int i=0; i<test_num; i++){
        cin >> day_num;
        for(int day = 0; day < day_num; day++){
            cin >> stock_value[day];
        }

        profit_sum = 0;
        max_stock = 0;
        for(int day = day_num-1; day>=0; day--){
            if(stock_value[day] < max_stock)
                profit_sum += (max_stock - stock_value[day]);
            else
                max_stock = stock_value[day];
        }
        cout << profit_sum << '\n';
    }

    return 0;
}
728x90
반응형