백준문제(13305)

·2025년 4월 25일

알고리즘문제

목록 보기
3/4


백준

풀이

  1. 현재 기름의 가격을 변수선언
  2. 반복문을 돌며 현재 기름가격이 이전가격보다 싸다면 값 변경
  3. 변수로 지정한 기름값 * 지나가야할 도로의 길이 를 결과값에 더해준다
  4. 기름과 거리의 범위값이 높아 변수타입을 long으로 진행

내 코드

package problem.p13305;

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        long[] loads = new long[n-1];
        long[] price = new long[n];

        for(int i=0; i<n-1;i++){
            loads[i] = sc.nextInt();
        }
        for(int i=0; i<n;i++){
            price[i] = sc.nextInt();
        }
        long currentPrice = price[0];
        long result = (long) loads[0] * price[0];
        for(int i=1;i<n-1;i++){
            if(price[i] <= currentPrice){
                currentPrice = price[i];
            }
            result += loads[i] * currentPrice;
        }

        System.out.println(result);
    }
}

profile
진입니다.

0개의 댓글