[BOJ][๐ŸŸก5][๋ฐฑ์ค€#01041] ์ฃผ์‚ฌ์œ„

์ž‘์„ฑ:    

์—…๋ฐ์ดํŠธ:

์นดํ…Œ๊ณ ๋ฆฌ:

ํƒœ๊ทธ: , , ,

๋ฌธ์ œ ์ถœ์ฒ˜

BAEKJOON Online Judge #1041


๋ฌธ์ œ

+---+        
| D |         +---+---+---+---+ | E | A | B | F | +---+---+---+---+
| C |        
+---+        

์ฃผ์‚ฌ์œ„๋Š” ์œ„์™€ ๊ฐ™์ด ์ƒ๊ฒผ๋‹ค. ์ฃผ์‚ฌ์œ„์˜ ์—ฌ์„ฏ ๋ฉด์—๋Š” ์ˆ˜๊ฐ€ ์“ฐ์—ฌ ์žˆ๋‹ค. ์œ„์˜ ์ „๊ฐœ๋„๋ฅผ ์ˆ˜๊ฐ€ ๋ฐ–์œผ๋กœ ๋‚˜์˜ค๊ฒŒ ์ ‘๋Š”๋‹ค. A, B, C, D, E,ย F์— ์“ฐ์—ฌ ์žˆ๋Š” ์ˆ˜๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ง€๋ฏผ์ด๋Š” ํ˜„์žฌ ๋™์ผํ•œ ์ฃผ์‚ฌ์œ„๋ฅผ N3๊ฐœ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ์ด ์ฃผ์‚ฌ์œ„๋ฅผ ์ ์ ˆํžˆ ํšŒ์ „์‹œํ‚ค๊ณ  ์Œ“์•„์„œ, Nร—Nร—Nํฌ๊ธฐ์˜ ์ •์œก๋ฉด์ฒด๋ฅผ ๋งŒ๋“ค๋ ค๊ณ  ํ•œ๋‹ค. ์ด ์ •์œก๋ฉด์ฒด๋Š” ํƒ์ž์œ„์— ์žˆ์œผ๋ฏ€๋กœ, 5๊ฐœ์˜ ๋ฉด๋งŒ ๋ณด์ธ๋‹ค. N๊ณผ ์ฃผ์‚ฌ์œ„์— ์“ฐ์—ฌ ์žˆ๋Š” ์ˆ˜๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ, ๋ณด์ด๋Š” 5๊ฐœ์˜ ๋ฉด์— ์“ฐ์—ฌย ์žˆ๋Š” ์ˆ˜์˜ ํ•ฉ์˜ ์ตœ์†Ÿ๊ฐ’์„ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.


์ž…๋ ฅ

์ฒซ์งธ ์ค„์— N์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„์— ์ฃผ์‚ฌ์œ„์— ์“ฐ์—ฌ ์žˆ๋Š” ์ˆ˜๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์—์„œ A, B, C, D, E, F์— ์“ฐ์—ฌ ์žˆ๋Š” ์ˆ˜๊ฐ€ ์ฐจ๋ก€๋Œ€๋กœ ์ฃผ์–ด์ง„๋‹ค. N์€ 1,000,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๊ณ , ์“ฐ์—ฌ ์žˆ๋Š” ์ˆ˜๋Š” 50๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๋‹ค.


์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ๋ฌธ์ œ์˜ ์ •๋‹ต์„ ์ถœ๋ ฅํ•œ๋‹ค.


์˜ˆ์ œ

์˜ˆ์ œ 1

์ž…๋ ฅ

2
1 2 3 4 5 6


์ถœ๋ ฅ

36


์˜ˆ์ œ 2

์ž…๋ ฅ

3
1 2 3 4 5 6


์ถœ๋ ฅ

69


์˜ˆ์ œ 3

์ž…๋ ฅ

1000000
50 50 50 50 50 50


์ถœ๋ ฅ

250000000000000


์˜ˆ์ œ 4

์ž…๋ ฅ

10
1 1 1 1 50 1


์ถœ๋ ฅ

500


My Sol

import sys
from itertools import combinations

def main():
    def calc_min_single():
        return min(arr)

    def calc_min_double():
        S = {comb for comb in combinations(range(6), 2)}
        del_S = {(0, 5), (1, 4), (2, 3)}
        minn = 1000
        for a, b in S - del_S:
            minn = min(minn, arr[a]+arr[b])
        return minn

    def calc_min_triple():
        S = {(0,1,2),(0,1,3),(0,4,2),(0,4,3), (5,1,2),(5,1,3),(5,4,2),(5,4,3)}
        minn = 1000
        for a, b, c in S:
            minn = min(minn, arr[a]+arr[b]+arr[c])
        return minn

    def check_outer():
        min_single = calc_min_single()
        min_double = calc_min_double()
        min_triple = calc_min_triple()

        sq_triple = 4
        sq_double = 4*(N-2)+4*(N-1)
        sq_single = 4*(N-1)*(N-2)+(N-2)**2

        single = min_single * sq_single
        double = min_double * sq_double
        triple = min_triple * sq_triple
        return single + double + triple


    N = int(input())
    arr = list(map(int, input().split()))
    if N==1: return sum(arr)-max(arr)
    return check_outer()

print(main())

๋ฐ‘๋ฉด์„ ์ œ์™ธํ•˜๊ณ  ์˜†๋ฉด๊ณผ ์œ—๋ฉด์œผ๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋ ค ํ–ˆ๋‹ค. ๊ฐ€์žฅ ์œ„์˜ 4๊ฐœ์˜ ๊ผญ์ง“์ ๋งŒ 3๊ฐœ์˜ ๋ฉด์„ ๊ฐ€์ง€๊ณ , 4๊ฐœ์˜ ์„ธ๋กœ ๋ณ€์€ 2๊ฐœ์˜ ๋ฉด, ๊ทธ๋ฆฌ๊ณ  ๋‚˜๋จธ์ง€๋Š” ํ•œ ๊ฐœ์˜ ๋ฉด์„ ๊ฐ€์ง„๋‹ค๋Š” ์ ์—์„œ ํ’€์ด๋ฅผ ์ฐฉ์•ˆํ–ˆ๋‹ค.

  1. ์ž…๋ ฅ์œผ๋กœ N๊ณผ 6๊ฐœ์˜ ์ˆ˜ arr์„ ๋ฐ›์•„ ์ฒ˜๋ฆฌํ•œ๋‹ค.
  2. calc_min_single ํ•จ์ˆ˜์—์„œ 1๊ฐœ ๋ฉด์„ ๋ณด์ด๋Š” ๊ฒฝ์šฐ์˜ ์ตœ์†Ÿ๊ฐ’์„ ๊ณ„์‚ฐํ•œ๋‹ค. ์ด๋Š” ๋‹จ์ˆœํžˆ arr์˜ ์ตœ์†Ÿ๊ฐ’์„ ์ทจํ•˜๋ฉด ๋˜๊ฒ ๋‹ค.
  3. calc_min_double ํ•จ์ˆ˜์—์„œ 2๊ฐœ ๋ฉด์„ ๋ณด์ด๋Š” ๊ฒฝ์šฐ์˜ ์ตœ์†Ÿ๊ฐ’์„ ๊ณ„์‚ฐํ•œ๋‹ค. ํ•˜๋‚˜ํ•˜๋‚˜ ํ•˜๋“œ์ฝ”๋”ฉํ•˜๋ฉด ์ด 4+4+4๊ฐœ์˜ ๊ฒฝ์šฐ์˜ ์ˆ˜๊ฐ€ ๋‚˜์˜ค์ง€๋งŒ ํ•˜๋‚˜ํ•˜๋‚˜ ์“ฐ๊ธฐ๊ฐ€ ์‹ซ์–ด์„œ itertools์˜ combinations ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ 6C2๋ฅผ ๊ตฌํ•˜๊ณ , ์„œ๋กœ ๋งˆ์ฃผ๋ณด๋Š” 3๊ฐ€์ง€ ๊ฒฝ์šฐ๋ฅผ ๋นผ์ฃผ์—ˆ๋‹ค. 12๊ฐ€์ง€ ๊ฒฝ์šฐ์—์„œ ๊ฐ€์žฅ ์ž‘์€ ํ•ฉ์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  4. calc_min_triple ํ•จ์ˆ˜์—์„œ 3๊ฐœ ๋ฉด์„ ๋ณด์ด๋Š” ๊ฒฝ์šฐ์˜ ์ตœ์†Ÿ๊ฐ’์„ ๊ณ„์‚ฐํ•œ๋‹ค. ์ด๋Š” ์–ด์ฉ” ์ˆ˜ ์—†์ด ํ•˜๋“œ์ฝ”๋”ฉํ•ด์ฃผ์—ˆ๊ณ , 8๊ฐœ์˜ ๊ฒฝ์šฐ์˜์ˆ˜์ด๋ฉด ๋˜๋ฏ€๋กœ ํฌ๊ฒŒ ๋ฌธ์ œ๊ฐ€ ๋˜์ง€ ์•Š์•˜๋‹ค. ์œ„์ฒ˜๋Ÿผ ํ•ฉ์˜ ์ตœ์†Ÿ๊ฐ’์„ ๊ตฌํ•ด ๋ฐ˜ํ™˜ํ•˜์˜€๋‹ค.
  5. check_outer ํ•จ์ˆ˜์—์„œ 5๊ฐœ ๋ฉด์—์„œ ๊ตฌํ•ด์ ธ์•ผ ํ•˜๋Š” 1๊ฐœ๋ฉด, 2๊ฐœ๋ฉด, 3๊ฐœ๋ฉด์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•ด ๊ณฑํ•ด์ฃผ์—ˆ๋‹ค. ์ด ๊ฐ๊ฐ์„ single, double, triple๋กœ ์ง€์ •ํ•˜์˜€๊ณ , ์ด๋ฅผ ๋”ํ•œ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜์˜€๋‹ค.
  6. N์ด 1์ธ ํŠน์ด ์ผ€์ด์Šค์—์„œ๋งŒ ์ „์ฒด ํ•ฉ์—์„œ ์ตœ๋Œ“๊ฐ’์„ ๋นผ์ค€ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๊ณ , ๋‚˜๋จธ์ง€๋Š” check_outer ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜๊ฐ’์œผ๋กœ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ์–ด์„œ ๋ถ„๊ธฐ์ฒ˜๋ฆฌํ•ด ๋ฐ˜ํ™˜ํ•˜๊ณ , main ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜๊ฐ’์„ ์ตœ์ข…์ ์œผ๋กœ ์ถœ๋ ฅํ•ด์ค€๋‹ค.


๊ฒฐ๊ณผ

๋งž์•˜์Šต๋‹ˆ๋‹ค!!


๋ชจ๋ฒ”๋‹ต์•ˆ

์ถœ์ฒ˜

n=int(input())
a,b,c,d,e,f=map(int,input().split())
if n==1:
	print(a+b+c+d+e+f-max(a,b,c,d,e,f))
else:
	print(min(a,b,c,d,e,f)*(5*n*n-16*n+12)+min(a+c,a+b,a+d,a+e,b+c,b+f,b+d,c+e,c+f,d+e,d+f,e+f)*(8*n-12)+min(a+b+c,a+b+d,a+d+e,a+e+c,b+d+f,b+c+f,c+e+f,d+e+f)*4)

์‚ฌ์‹ค ๋ชจ๋ฒ”๋‹ต์•ˆ์€ ์•„๋‹ˆ๊ณ  ์ฝ”๋“œ๊ธธ์ด์™€ ์—ฐ์‚ฐ์‹œ๊ฐ„์ด ์งง์€ ํ’€์ด๋ฅผ ๊ฐ€์ ธ์™€๋ณด์•˜๋‹ค. ์‚ฌ์‹ค ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๋ชจ๋‘ ํ•˜๋“œ์ฝ”๋”ฉ์œผ๋กœ ์ž‘์„ฑํ•˜์—ฌ minimum์„ ์ฐพ์œผ๋ฉด ๋˜๋Š” ๋ฌธ์ œ๊ธด ํ–ˆ๋‹ค. ๋ฌธ์ œ๋ฅผ ํ’€๋ฉด์„œ๋„ ์ด๊ฒŒ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์š”๊ตฌํ•˜๋‚˜..? ํ•˜๋Š” ์ƒ๊ฐ์„ ์ข€ ํ•˜๊ธด ํ–ˆ๋‹ค. ์•„๋ฌดํŠผ ๊ทธ๊ฑด ๊ทธ๊ฑฐ๊ณ  ํ•˜๋“œ์ฝ”๋”ฉ์ธ ์ ์„ ์ œ์™ธํ•˜๋ฉด ๋‚˜๋ฆ„ ์ง๊ด€์ ์ด๊ณ  ์‰ฝ๊ฒŒ ํ’€์–ด๋‚ธ ํ’€์ด์˜€๋‹ค.

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ