[그리디 - 13305번] 주유소

Jeong Ha Seung·2022년 1월 21일

import sys

n = int(input())  # 도시의 개수
road = list(map(int, input().split()))  # 도로의 길이
cost = list(map(int, input().split()))  # 주유소의 리터 당 가격

result = 0
first_cost = cost[0]

for i in range(n-1):
    if cost[i] < first_cost:
        first_cost = cost[i]
    result += first_cost*road[i]

print(result)

for문에서 n-1로 한 이유는 마지막 도시에서의 기름값은 의미가 없기 때문이다.

다음번 도시에 넣을 기름 가격이 첫 번째 도시에서 넣은 기름의 가격보다 작을 경우, 결과갑을 해당값으로 업데이트 해준다.

profile
블로그 이전했습니다. https://morethan-haseung-log.vercel.app

0개의 댓글