백준 13305 greedy 주유소

자이로 체펠리·2021년 7월 29일

문제

풀이

문제 내용은 그렇게 어렵지 않다. 현재노드와 지금까지 최소 가격을 비교하며 다음 노드까지의 거리를 곱해주면 됬다. 다만 서브태스크가 존재 하기 때문에 데이터 타입을 int가 아닌 long을 사용해야지 100점을 맞을 수 있다.

코드

import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        long[] len = new long[n-1];
        long[] price = new long[n];
        for(int i = 0; i<n-1;i++){
            len[i]= sc.nextInt();
        }
        for(int i = 0 ; i<n; i++){
            price[i]= sc.nextInt();
        }
        long min = Long.MAX_VALUE;
        long sum = 0;
        for(int i = 0; i<n-1;i++){
            if(min > price[i])min = price[i];
            sum += min*len[i];
        }
        System.out.println(sum);
    }
}
profile
"경의를 표해라. 경의를 갖고 회전의 다음 단계로 나아가는 거다…… [LESSON 4] 다."

0개의 댓글