상세 컨텐츠

본문 제목

백준 13305번

백준 알고리즘 풀이

by 발발개발 2021. 4. 5. 11:30

본문

원본 : www.acmicpc.net/problem/13305

 

13305번: 주유소

표준 입력으로 다음 정보가 주어진다. 첫 번째 줄에는 도시의 개수를 나타내는 정수 N(2 ≤ N ≤ 100,000)이 주어진다. 다음 줄에는 인접한 두 도시를 연결하는 도로의 길이가 제일 왼쪽 도로부터 N-1

www.acmicpc.net

 

풀이

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.math.BigDecimal;
import java.util.Arrays;

public class Main {

	public static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
	public static BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

	public static void main(String[] args) throws Exception {
		int n = Integer.parseInt(reader.readLine());
		long length[] = Arrays.stream(reader.readLine().split(" ")).mapToLong(Long::parseLong).toArray();
		long cost[] = Arrays.stream(reader.readLine().split(" ")).mapToLong(Long::parseLong).toArray();
		long result = 0;
		long currentCost = 0;
		
		for (int i = 0; i < n - 1; i++) {
			if (i == 0) {
				currentCost = cost[i];
				result += cost[i] * length[i];
			} else {
				if (cost[i] < currentCost) {
					currentCost = cost[i];
					result += cost[i] * length[i];
				} else {
					result += currentCost * length[i];
				}
			}
		}
		
		BigDecimal bigDecimal = new BigDecimal(result);
		writer.write(bigDecimal.toString());
		writer.flush();
	}
	
}

'백준 알고리즘 풀이' 카테고리의 다른 글

백준 1037번  (0) 2021.04.05
백준 5086번  (0) 2021.04.05
백준 1541번  (0) 2021.04.05
백준 18870번  (0) 2021.04.05
백준 11399번  (0) 2021.04.02

관련글 더보기

댓글 영역