11501(주식)
#define _CRT_SECURE_NO_WARNINGS
#include <string>
#include <vector>
#include <queue>
#include <memory.h>
#include <algorithm>
#include <iostream>
#include <stack>
#include <unordered_set>
#include <math.h>
using namespace std;
int a[1000000] = { 0 };
int main(void)
{
cin.tie(NULL);
cout.tie(NULL);
ios_base::sync_with_stdio(false);
freopen("input.txt", "r", stdin);
int ts;
cin >> ts;
int n;
while (ts--)
{
cin >> n;
long long answer = 0;
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
int mmax = a[n - 1];
for (int i = n - 2; i >= 0; i--)
{
mmax = max(a[i], mmax);
if (a[i] < mmax)
answer += mmax - a[i];
}
cout << answer << "\n";
}
}
고찰
문제는 간단하다 주식 장에서 얻을 수 있는 최대 이익을 구하면 된다.
거꾸로 탐색하면서 max값을 갱신한다. 동시에 현재 배열값보다 max값이 크면 그 차이를 answer에 더해준다.