[Codeforces] Kotlin Heroes: Episode 4 B. Boot Camp

Kotlin Heroes: Episode 4 B. Boot Camp

  • Time : O(n)
  • Space : O(1)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.util.*

fun solve(n: Int, x: Int, y: Int, s: String) : Int {
var res = 0;
var last = 0;
for(ch in s) {
if(ch == '0') last = 0;
else last = (y - last).coerceAtMost(x);
res += last;
}
return res;
}

fun main(args: Array<String>) {
val r = Scanner(System.`in`)
val tc = r.nextInt();
for(i in 1 .. tc) {
val n = r.nextInt();
val x = r.nextInt();
val y = r.nextInt();
val s = r.next();
println(solve(n,x,y,s))
}
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/06/23/PS/Codeforces/kotlin-heroes-episode-4/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.