[BOJ] 10819번 : 차이를 최대로

문제 보기

소스

kotlin

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
import java.util.*
import kotlin.math.abs
import kotlin.math.max

var n = 0
val nums = IntArray(10)
val visited = BooleanArray(10)
var result = 0

fun main() = with(Scanner(System.`in`)) {
n = nextInt()
for (i in 0 until n) {
nums[i] = nextInt()
}
val arr = mutableListOf<Int>()
dfs(arr, 0)
println(result)
}

fun dfs(arr: MutableList<Int>, count: Int) {
if (count == n) {
result = max(arr.value(), result)
return
}

for (i in 0 until n) {
if (!visited[i]) {
visited[i] = true
arr.add(nums[i])
dfs(arr, count + 1)
arr.removeAt(arr.size - 1)
visited[i] = false
}
}
}

fun MutableList<Int>.value() : Int {
var sum = 0
for (i in 0 until n - 1) {
sum += abs(this[i] - this[i + 1])
}
return sum
}

댓글