알고리즘/백준
[백준] 11501. 주식
빙빙
2021. 4. 17. 18:00
1.거꾸로 봐서 현재보다 큰 것이 있다면 M 갱신.
2. 큰 것이 없다면 M에서 빼준 값을 누적 합을 구해주기
*****이 문제는 SWEA에 있는 1859번 백만장자 프로젝트 문제와 같은 문제이다*****
T = int(input())
for tc in range(T):
N = int(input())
lst = list(map(int, input().split()))
lst.reverse() #거꾸로 보기
M = lst[0]
ans = 0
for i in range(len(lst)):
if M >= lst[i]:
ans += M - lst[i]
else:
M = lst[i] # 큰게 있다면 최고가 갱신
print(ans)
런타임에러 난 코드(인덱스 에러)
##########런타임에러#########
T = int(input())
for tc in range(T):
N = int(input())
lst = list(map(int, input().split()))
ans = 0
while len(lst) > 1:
M = max(lst) #최고가,갱신
idx = lst.index(M)
if idx == 0:
lst.pop(0)
continue
sum_idx = 0
for i in range(idx):
sum_idx += lst[i]
ans += M * idx - sum_idx
# 제거해주기
for i in range(idx):
lst.pop(i)
lst.pop(0)
print(ans)