-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabc117_b.kt
30 lines (25 loc) · 852 Bytes
/
abc117_b.kt
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
fun main(args: Array<String>) {
val flowerCount = readLine()!!.toInt()
val flowers = readLine()!!.split(" ").map { it.toInt() }.toMutableList()
var takeCount = 0
fun takeWater(l: Int, r: Int) {
if (l > r) return
if (r >= flowerCount) return
for (i in l..r) {
flowers[i]--
}
takeCount++
}
while (true) {
if (flowers.count { it == 0 } == flowers.size) break
val firstFlowerIndex = flowers.indexOfFirst { it >= 1 }
val selectedHeight = flowers[firstFlowerIndex]
for (searchIndex in firstFlowerIndex + 1..flowerCount) {
if (searchIndex == flowerCount || flowers[searchIndex] < selectedHeight) {
takeWater(firstFlowerIndex, searchIndex - 1)
break
}
}
}
print(takeCount)
}