728x90
import sys

N = sys.stdin.readline()
data = list(map(int, sys.stdin.readline().split()))

data.sort()

def next_permutation(list):
    last_asc_idx = 0
    for i in range(len(list) - 1):
        if list[i] < list[i + 1]:
            last_asc_idx = i

    val_at_last_asc_idx = list[last_asc_idx]
    last_idx_larger_than_val_at_last_asc_idx = 0
    for j in range(last_asc_idx + 1, len(list), 1):
        if val_at_last_asc_idx < list[j]:
            last_idx_larger_than_val_at_last_asc_idx = j

    if last_asc_idx == 0 and last_idx_larger_than_val_at_last_asc_idx == 0:
        return False
    else:
        list[last_asc_idx], list[last_idx_larger_than_val_at_last_asc_idx] = \\
            list[last_idx_larger_than_val_at_last_asc_idx], list[last_asc_idx]
        last_idx_larger_than_val_at_last_asc_idx = len(list) - 1
        while last_asc_idx + 1 < last_idx_larger_than_val_at_last_asc_idx:
            list[last_asc_idx + 1], list[last_idx_larger_than_val_at_last_asc_idx] = \\
                list[last_idx_larger_than_val_at_last_asc_idx], list[last_asc_idx + 1]
            last_asc_idx += 1;
            last_idx_larger_than_val_at_last_asc_idx -= 1
        return True

def get_res(list):
    res = 0
    for i in range(len(list) - 1):
        if list[i] < list[i + 1]:
            res += list[i + 1] - list[i]
        else:
            res += list[i] - list[i + 1]
    return res

max_res = get_res(data)
while next_permutation(data):
    tmp_res = get_res(data)
    if max_res < tmp_res:
        max_res = tmp_res

print(max_res)
  • 포인트
    • bruteforce 파악
728x90

'Algorithm-Problems > 백준' 카테고리의 다른 글

[백준][11724] 연결 요소의 개수  (0) 2022.05.08
[백준][10972] 다음 순열  (0) 2022.05.08
[백준][9613] GCD 합  (0) 2022.05.08
[백준][9095] 1, 2, 3 더하기  (0) 2022.05.08
[백준][6603] 로또  (0) 2022.05.08

+ Recent posts