본문 바로가기

IT/알고리즘

[백준] 차이를 최대로

728x90
SMALL

 

https://www.acmicpc.net/problem/10819

 

fun main() = with(Scanner(System.`in`)) {
    val count = nextInt()

    val visited = Array<Boolean>(count) { false }
    val array = Array(count) { 0 }
    for (i in array.indices) {
        array[i] = nextInt()
    }

    var result = 0
    fun dfs(depth: Int, total: Int , current : Int) {
        if (depth == count - 1) {
            result = max(result, total)
            return
        }

        for (i in 0 until count) {
            if (visited[i]) continue
            visited[i] = true
            dfs(depth = depth + 1, total = total + abs(current - array[i]) , current = array[i])
            visited[i] = false
        }
    }
    for(i in 0 until count){
        visited[i] = true
        dfs(depth = 0, total = 0 , current = array[i])
        visited[i] = false
    }

    println(result)
}
728x90
LIST

'IT > 알고리즘' 카테고리의 다른 글

[백준] 2xn 타일링  (0) 2024.11.18
[백준] 2xn 타일링2  (0) 2024.11.18
[백준] 퇴사  (1) 2024.11.12
[백준] 좋은수열  (0) 2024.11.11
[백준] 단어수학  (2) 2024.11.11