-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13305.java
More file actions
51 lines (49 loc) · 1.45 KB
/
13305.java
File metadata and controls
51 lines (49 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
거리: 2 3 1 2
주유 비용: 5 2 4 7 8
거리:2 거리:3 거리:1 거리:2
[5원] [2원] [4원] [7원] [8원]
=> "뒤에 있는" 자기보다 비싼 주유소를 미리 "결제"한다.
[핵심] 비오름차순 처리하기
예시 1)
주유 비용: 5 2 4 7 8
실제 주유 비용: 5 2 2 2 2
예시 2)
거리: 2 3 1 2
주유 비용: 7 5 8 4 9
실제 주유 비용: 7 5 5 4 4
답: (7 * 2) + (5 * 3) + (5 * 1) + (4 * 2)
[아이디어 핵심] 비오름차순으로 바꾸어주기
*/
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 전체 주유소의 수
int n = scanner.nextInt();
int[] distances = new int[n - 1];
int[] costs = new int[n];
// 전체 거리(distance) 입력받기
for (int i = 0; i < n - 1; i++) {
distances[i] = scanner.nextInt();
}
// 전체 비용(cost) 입력받기
for (int i = 0; i < n; i++) {
costs[i] = scanner.nextInt();
}
// 실제 비용은 비오름차순으로 재정의
int[] realCosts = new int[n];
int minValue = costs[0];
for (int i = 0; i < n; i++) {
if (minValue > costs[i]) minValue = costs[i];
realCosts[i] = minValue;
}
// 실제 주유 비용 계산
long result = 0;
for (int i = 0; i < n - 1; i++) {
result += (long) distances[i] * realCosts[i];
}
System.out.println(result);
}
}