사용 알고리즘 : 그리디...
사용 언어: java
package week03;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Main_bj_1041_주사위 {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
long n = Integer.parseInt(br.readLine()); //사용할 주사위 개수
StringTokenizer st = new StringTokenizer(br.readLine());
long[] dice = new long[6];
for (int i=0; i<6; i++) {
dice[i] = Integer.parseInt(st.nextToken());
}
// 입력 끝
/**
* 3면이 보임: 4개
* 2면이 보임: 4(n-2) + 4(n-1) = 8n-12개
* 1면이 보임: {n^2 - 4 - 4(n-2)} + 4{n^2 - n - 2(n-1)} = 5n^2 - 16n + 12 개
*/
long ans = 0; //겉에 쓰인 숫자 합 최솟값
long[] mins = new long[3];
mins[0] = (int) Math.min(dice[0], dice[5]);
mins[1] = (int) Math.min(dice[1], dice[4]);
mins[2] = (int) Math.min(dice[2], dice[3]);
Arrays.sort(mins);
ans += ( 5*n*n - 16*n + 12 ) * mins[0] //1면
+ ( 8*n - 12 ) * ( mins[0] + mins[1] ) //2면
+ 4 * ( mins[0] + mins[1] + mins[2] ); //3면
// n이 1일 떄를 처리하지 않으면 틀린다
if(n==1) {
Arrays.sort(dice);
System.out.println(dice[0]+dice[1]+dice[2]+dice[3]+dice[4]);
}
else System.out.println(ans);
}
}
곤란한다 곤란해..
n == 1일 경우를 꼭 처리해야 함!
'알고리즘 > 백준' 카테고리의 다른 글
14890_경사로 (1) | 2024.06.15 |
---|---|
12100_2048 (Easy) (1) | 2024.06.14 |
14891_톱니바퀴 (0) | 2024.06.13 |
3190_뱀 (1) | 2024.06.13 |
1062_가르침 ** (0) | 2024.06.13 |