[BOJ] 그리디 알고리즘/13305번 - 주유소

aio·2021년 2월 18일
0

백준 알고리즘

목록 보기
9/9

문제

제출답안

	import java.util.*;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int cityNum = sc.nextInt();
		
		long[] distance = new long[cityNum-1];
		for(int i=0; i<distance.length; i++) {	// 2 3 1
			distance[i] = sc.nextLong();
		}
		
		long[] oilPrice = new long[cityNum];
		for(int i=0; i<oilPrice.length; i++) {	// 5 2 4 1
			oilPrice[i] = sc.nextLong();
		}
					
		long sumPrice = 0;
		long tempPrice = oilPrice[0];
		
		for(int i=0; i<oilPrice.length-1; i++) {	// 도착지점 기름값은 필요없으므로 -1번 수행
			if(tempPrice > oilPrice[i]) {			// 리터당 기름 값이 내림차순일 경우 주유하면 최소비용이 됨
				tempPrice = oilPrice[i];			// 비교하는 기릅 값이 오름차순일 경우 내림차순이 되도록 비용 변경
			}
			sumPrice += tempPrice * distance[i]; 
		}
		
		System.out.println(sumPrice);
		
		sc.close();
	}

}

> 출처

[백준 알고리즘](https://www.acmicpc.net/problem/13305)

0개의 댓글