한량처럼 살고 싶다

1859. 백만 장자 프로젝트(Python) 본문

PS/SWEA

1859. 백만 장자 프로젝트(Python)

투영 2023. 10. 19. 18:59

https://swexpertacademy.com/main/code/problem/problemDetail.do

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

뒤에서부터 가격을 체크해야하는 것이 포인트이다.

def solution(T):
 
    for i in range(T):
        length = int(input())
        arr = list(map(int, input().split()))
 
        answer = 0 #최종 금액
        price = 0 #내가 소비한 가격
        count = 0 #내가 산 개수
         
        biggest_price = arr[length-1]
 
        for index in range(length-2, -1, -1):
            if arr[index] < biggest_price:
                count += 1 #구매 개수
                price -= arr[index] #소비 금액
             
            else:
                earn = biggest_price * count
                earn += price
                answer += earn
                biggest_price = arr[index]
                count = 0
                price = 0
         
        if count:
            answer += ((biggest_price*count)+price)
 
        print("#%d %d" %(i+1, answer))
 
T= int(input())
solution(T)