package ru.amai.study.hackerrank.practice.interviewPreparationKit.search.candies

import java.math.BigDecimal
import java.math.BigDecimal.ONE
import java.math.BigDecimal.ZERO
import java.math.BigDecimal.valueOf
import java.math.RoundingMode.CEILING
import java.math.RoundingMode.FLOOR
import java.util.*

fun minimumPasses(machines: Long, workers: Long, price: Long, targetCnt: Long): Long {
    var currentCandies = ZERO
    var days = ZERO

    val priceDecimal = price.toBigDecimal()
    val targetDecimal = targetCnt.toBigDecimal()

    var factoryResources = FactoryResources(
        valueOf(workers),
        valueOf(machines)
    )
    while (currentCandies < targetDecimal) {
        val produced = factoryResources.production
        currentCandies += produced

        val (newResources, cost) = investPlan(
            factoryResources,
            currentCandies,
            priceDecimal
        )

        val waitIfStore =
            (targetDecimal - currentCandies).divide(produced, CEILING)

        if (cost > ZERO) {
            val waitIfInvest =
                (targetDecimal - currentCandies + cost).divide((newResources.production), CEILING)

            if ((waitIfStore > ZERO) && (waitIfInvest <= waitIfStore)) {
                factoryResources = newResources
                currentCandies -= cost
                days++
            } else {
                days += (waitIfStore + ONE)
                currentCandies = targetDecimal
            }
        } else {
            val waitToInvest = (priceDecimal - currentCandies).divide(produced, CEILING)

            if (waitIfStore <= waitToInvest) {
                days += (waitIfStore + ONE)
                currentCandies = targetDecimal
            } else {
                days += waitToInvest
                currentCandies += produced * (waitToInvest - ONE)
            }
        }
    }

    return days.toLong()
}

private data class FactoryResources(
    val workers: BigDecimal,
    val machines: BigDecimal
)

private val FactoryResources.production: BigDecimal
    get() = workers * machines

private data class InvestPlan(
    val factoryResources: FactoryResources,
    val cost: BigDecimal
)

private val TWO = valueOf(2)

private fun investPlan(factoryResources: FactoryResources, candies: BigDecimal, price: BigDecimal): InvestPlan {
    val (currentWorkers, currentMachines) = factoryResources

    val canBuy = candies.divide(price, FLOOR)

    val diff = (currentMachines - currentWorkers).abs()
    val toBuyForBalance = diff.min(canBuy)
    val leftToBuy = canBuy - toBuyForBalance

    var newMachines = currentMachines
    var newWorkers = currentWorkers

    if (currentMachines > currentWorkers) {
        newWorkers += toBuyForBalance
    } else if (currentWorkers > currentMachines) {
        newMachines += toBuyForBalance
    }

    if (newMachines == newWorkers) {
        newMachines += leftToBuy.divide(TWO, FLOOR)
        newWorkers += leftToBuy.divide(TWO, CEILING)
    }

    val cost = ((newMachines - currentMachines) + (newWorkers - currentWorkers)) * price

    return InvestPlan(
        FactoryResources(newWorkers, newMachines),
        cost
    )
}

fun main() {
    val scan = Scanner(System.`in`)

    val (machines, workers, price, targetCnt) =
        scan.nextLine().split(" ").map { it.trim().toLong() }

    val result = minimumPasses(machines, workers, price, targetCnt)
    println(result)
}

Compiled from "MinimumPasses.kt"
public final class ru.amai.study.hackerrank.practice.interviewPreparationKit.search.candies.MinimumPassesKt {
  private static final java.math.BigDecimal TWO;

  public static final long minimumPasses(long, long, long, long);
    Code:
       0: getstatic     #12                 // Field java/math/BigDecimal.ZERO:Ljava/math/BigDecimal;
       3: astore        8
       5: getstatic     #12                 // Field java/math/BigDecimal.ZERO:Ljava/math/BigDecimal;
       8: astore        9
      10: lload         4
      12: invokestatic  #16                 // Method java/math/BigDecimal.valueOf:(J)Ljava/math/BigDecimal;
      15: dup
      16: ldc           #18                 // String valueOf(...)
      18: invokestatic  #24                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
      21: astore        10
      23: lload         6
      25: invokestatic  #16                 // Method java/math/BigDecimal.valueOf:(J)Ljava/math/BigDecimal;
      28: dup
      29: ldc           #18                 // String valueOf(...)
      31: invokestatic  #24                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
      34: astore        11
      36: new           #26                 // class ru/amai/study/hackerrank/practice/interviewPreparationKit/search/candies/FactoryResources
      39: dup
      40: lload_2
      41: invokestatic  #16                 // Method java/math/BigDecimal.valueOf:(J)Ljava/math/BigDecimal;
      44: dup
      45: ldc           #18                 // String valueOf(...)
      47: invokestatic  #24                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
      50: lload_0
      51: invokestatic  #16                 // Method java/math/BigDecimal.valueOf:(J)Ljava/math/BigDecimal;
      54: dup
      55: ldc           #18                 // String valueOf(...)
      57: invokestatic  #24                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
      60: invokespecial #30                 // Method ru/amai/study/hackerrank/practice/interviewPreparationKit/search/candies/FactoryResources."<init>":(Ljava/math/BigDecimal;Ljava/math/BigDecimal;)V
      63: astore        12
      65: aload         8
      67: aload         11
      69: invokevirtual #34                 // Method java/math/BigDecimal.compareTo:(Ljava/math/BigDecimal;)I
      72: ifge          539
      75: aload         12
      77: invokestatic  #38                 // Method getProduction:(Lru/amai/study/hackerrank/practice/interviewPreparationKit/search/candies/FactoryResources;)Ljava/math/BigDecimal;
      80: astore        13
      82: aload         8
      84: astore        14
      86: aload         14
      88: invokestatic  #42                 // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
      91: aload         14
      93: aload         13
      95: invokevirtual #46                 // Method java/math/BigDecimal.add:(Ljava/math/BigDecimal;)Ljava/math/BigDecimal;
      98: dup
      99: ldc           #48                 // String add(...)
     101: invokestatic  #24                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
     104: astore        8
     106: aload         12
     108: aload         8
     110: aload         10
     112: invokestatic  #52                 // Method investPlan:(Lru/amai/study/hackerrank/practice/interviewPreparationKit/search/candies/FactoryResources;Ljava/math/BigDecimal;Ljava/math/BigDecimal;)Lru/amai/study/hackerrank/practice/interviewPreparationKit/search/candies/InvestPlan;
     115: astore        14
     117: aload         14
     119: invokevirtual #58                 // Method ru/amai/study/hackerrank/practice/interviewPreparationKit/search/candies/InvestPlan.component1:()Lru/amai/study/hackerrank/practice/interviewPreparationKit/search/candies/FactoryResources;
     122: astore        15
     124: aload         14
     126: invokevirtual #62                 // Method ru/amai/study/hackerrank/practice/interviewPreparationKit/search/candies/InvestPlan.component2:()Ljava/math/BigDecimal;
     129: astore        16
     131: aload         11
     133: aload         8
     135: invokevirtual #65                 // Method java/math/BigDecimal.subtract:(Ljava/math/BigDecimal;)Ljava/math/BigDecimal;
     138: dup
     139: ldc           #67                 // String subtract(...)
     141: invokestatic  #24                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
     144: aload         13
     146: getstatic     #73                 // Field java/math/RoundingMode.CEILING:Ljava/math/RoundingMode;
     149: invokevirtual #77                 // Method java/math/BigDecimal.divide:(Ljava/math/BigDecimal;Ljava/math/RoundingMode;)Ljava/math/BigDecimal;
     152: astore        17
     154: aload         16
     156: getstatic     #12                 // Field java/math/BigDecimal.ZERO:Ljava/math/BigDecimal;
     159: invokevirtual #34                 // Method java/math/BigDecimal.compareTo:(Ljava/math/BigDecimal;)I
     162: ifle          338
     165: nop
     166: aload         11
     168: aload         8
     170: invokevirtual #65                 // Method java/math/BigDecimal.subtract:(Ljava/math/BigDecimal;)Ljava/math/BigDecimal;
     173: dup
     174: ldc           #67                 // String subtract(...)
     176: invokestatic  #24                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
     179: aload         16
     181: invokevirtual #46                 // Method java/math/BigDecimal.add:(Ljava/math/BigDecimal;)Ljava/math/BigDecimal;
     184: dup
     185: ldc           #48                 // String add(...)
     187: invokestatic  #24                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
     190: aload         15
     192: invokestatic  #38                 // Method getProduction:(Lru/amai/study/hackerrank/practice/interviewPreparationKit/search/candies/FactoryResources;)Ljava/math/BigDecimal;
     195: getstatic     #73                 // Field java/math/RoundingMode.CEILING:Ljava/math/RoundingMode;
     198: invokevirtual #77                 // Method java/math/BigDecimal.divide:(Ljava/math/BigDecimal;Ljava/math/RoundingMode;)Ljava/math/BigDecimal;
     201: astore        18
     203: aload         17
     205: getstatic     #12                 // Field java/math/BigDecimal.ZERO:Ljava/math/BigDecimal;
     208: invokevirtual #34                 // Method java/math/BigDecimal.compareTo:(Ljava/math/BigDecimal;)I
     211: ifle          271
     214: aload         18
     216: aload         17
     218: invokevirtual #34                 // Method java/math/BigDecimal.compareTo:(Ljava/math/BigDecimal;)I
     221: ifgt          271
     224: aload         15
     226: astore        12
     228: aload         8
     230: aload         16
     232: invokevirtual #65                 // Method java/math/BigDecimal.subtract:(Ljava/math/BigDecimal;)Ljava/math/BigDecimal;
     235: dup
     236: ldc           #67                 // String subtract(...)
     238: invokestatic  #24                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
     241: astore        8
     243: aload         9
     245: astore        19
     247: aload         19
     249: invokestatic  #42                 // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
     252: aload         19
     254: getstatic     #80                 // Field java/math/BigDecimal.ONE:Ljava/math/BigDecimal;
     257: invokevirtual #46                 // Method java/math/BigDecimal.add:(Ljava/math/BigDecimal;)Ljava/math/BigDecimal;
     260: dup
     261: ldc           #48                 // String add(...)
     263: invokestatic  #24                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
     266: astore        9
     268: goto          65
     271: aload         9
     273: astore        19
     275: aload         19
     277: invokestatic  #42                 // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
     280: aload         19
     282: astore        19
     284: aload         17
     286: invokestatic  #42                 // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
     289: aload         17
     291: astore        20
     293: getstatic     #80                 // Field java/math/BigDecimal.ONE:Ljava/math/BigDecimal;
     296: dup
     297: ldc           #81                 // String ONE
     299: invokestatic  #24                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
     302: aload         20
     304: swap
     305: invokevirtual #46                 // Method java/math/BigDecimal.add:(Ljava/math/BigDecimal;)Ljava/math/BigDecimal;
     308: dup
     309: ldc           #48                 // String add(...)
     311: invokestatic  #24                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
     314: astore        20
     316: aload         19
     318: aload         20
     320: invokevirtual #46                 // Method java/math/BigDecimal.add:(Ljava/math/BigDecimal;)Ljava/math/BigDecimal;
     323: dup
     324: ldc           #48                 // String add(...)
     326: invokestatic  #24                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
     329: astore        9
     331: aload         11
     333: astore        8
     335: goto          65
     338: aload         10
     340: aload         8
     342: invokevirtual #65                 // Method java/math/BigDecimal.subtract:(Ljava/math/BigDecimal;)Ljava/math/BigDecimal;
     345: dup
     346: ldc           #67                 // String subtract(...)
     348: invokestatic  #24                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
     351: aload         13
     353: getstatic     #73                 // Field java/math/RoundingMode.CEILING:Ljava/math/RoundingMode;
     356: invokevirtual #77                 // Method java/math/BigDecimal.divide:(Ljava/math/BigDecimal;Ljava/math/RoundingMode;)Ljava/math/BigDecimal;
     359: astore        18
     361: aload         17
     363: aload         18
     365: invokevirtual #34                 // Method java/math/BigDecimal.compareTo:(Ljava/math/BigDecimal;)I
     368: ifgt          438
     371: aload         9
     373: astore        19
     375: aload         19
     377: invokestatic  #42                 // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
     380: aload         19
     382: astore        19
     384: aload         17
     386: invokestatic  #42                 // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
     389: aload         17
     391: astore        20
     393: getstatic     #80                 // Field java/math/BigDecimal.ONE:Ljava/math/BigDecimal;
     396: dup
     397: ldc           #81                 // String ONE
     399: invokestatic  #24                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
     402: aload         20
     404: swap
     405: invokevirtual #46                 // Method java/math/BigDecimal.add:(Ljava/math/BigDecimal;)Ljava/math/BigDecimal;
     408: dup
     409: ldc           #48                 // String add(...)
     411: invokestatic  #24                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
     414: astore        20
     416: aload         19
     418: aload         20
     420: invokevirtual #46                 // Method java/math/BigDecimal.add:(Ljava/math/BigDecimal;)Ljava/math/BigDecimal;
     423: dup
     424: ldc           #48                 // String add(...)
     426: invokestatic  #24                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
     429: astore        9
     431: aload         11
     433: astore        8
     435: goto          65
     438: aload         9
     440: astore        19
     442: aload         19
     444: invokestatic  #42                 // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
     447: aload         19
     449: astore        19
     451: aload         18
     453: invokestatic  #42                 // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
     456: aload         19
     458: aload         18
     460: invokevirtual #46                 // Method java/math/BigDecimal.add:(Ljava/math/BigDecimal;)Ljava/math/BigDecimal;
     463: dup
     464: ldc           #48                 // String add(...)
     466: invokestatic  #24                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
     469: astore        9
     471: aload         8
     473: astore        19
     475: aload         13
     477: astore        20
     479: aload         18
     481: astore        21
     483: getstatic     #80                 // Field java/math/BigDecimal.ONE:Ljava/math/BigDecimal;
     486: dup
     487: ldc           #81                 // String ONE
     489: invokestatic  #24                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
     492: aload         21
     494: swap
     495: invokevirtual #65                 // Method java/math/BigDecimal.subtract:(Ljava/math/BigDecimal;)Ljava/math/BigDecimal;
     498: dup
     499: ldc           #67                 // String subtract(...)
     501: invokestatic  #24                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
     504: astore        21
     506: aload         20
     508: aload         21
     510: invokevirtual #84                 // Method java/math/BigDecimal.multiply:(Ljava/math/BigDecimal;)Ljava/math/BigDecimal;
     513: dup
     514: ldc           #86                 // String multiply(...)
     516: invokestatic  #24                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
     519: astore        20
     521: aload         19
     523: aload         20
     525: invokevirtual #46                 // Method java/math/BigDecimal.add:(Ljava/math/BigDecimal;)Ljava/math/BigDecimal;
     528: dup
     529: ldc           #48                 // String add(...)
     531: invokestatic  #24                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
     534: astore        8
     536: goto          65
     539: aload         9
     541: invokevirtual #90                 // Method java/math/BigDecimal.longValue:()J
     544: lreturn

  private static final java.math.BigDecimal getProduction(ru.amai.study.hackerrank.practice.interviewPreparationKit.search.candies.FactoryResources);
    Code:
       0: aload_0
       1: invokevirtual #110                // Method ru/amai/study/hackerrank/practice/interviewPreparationKit/search/candies/FactoryResources.getWorkers:()Ljava/math/BigDecimal;
       4: aload_0
       5: invokevirtual #113                // Method ru/amai/study/hackerrank/practice/interviewPreparationKit/search/candies/FactoryResources.getMachines:()Ljava/math/BigDecimal;
       8: invokevirtual #84                 // Method java/math/BigDecimal.multiply:(Ljava/math/BigDecimal;)Ljava/math/BigDecimal;
      11: dup
      12: ldc           #86                 // String multiply(...)
      14: invokestatic  #24                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
      17: areturn

  private static final ru.amai.study.hackerrank.practice.interviewPreparationKit.search.candies.InvestPlan investPlan(ru.amai.study.hackerrank.practice.interviewPreparationKit.search.candies.FactoryResources, java.math.BigDecimal, java.math.BigDecimal);
    Code:
       0: aload_0
       1: invokevirtual #116                // Method ru/amai/study/hackerrank/practice/interviewPreparationKit/search/candies/FactoryResources.component1:()Ljava/math/BigDecimal;
       4: astore_3
       5: aload_0
       6: invokevirtual #117                // Method ru/amai/study/hackerrank/practice/interviewPreparationKit/search/candies/FactoryResources.component2:()Ljava/math/BigDecimal;
       9: astore        4
      11: aload_1
      12: aload_2
      13: getstatic     #120                // Field java/math/RoundingMode.FLOOR:Ljava/math/RoundingMode;
      16: invokevirtual #77                 // Method java/math/BigDecimal.divide:(Ljava/math/BigDecimal;Ljava/math/RoundingMode;)Ljava/math/BigDecimal;
      19: astore        5
      21: aload         4
      23: aload_3
      24: invokevirtual #65                 // Method java/math/BigDecimal.subtract:(Ljava/math/BigDecimal;)Ljava/math/BigDecimal;
      27: dup
      28: ldc           #67                 // String subtract(...)
      30: invokestatic  #24                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
      33: invokevirtual #123                // Method java/math/BigDecimal.abs:()Ljava/math/BigDecimal;
      36: astore        6
      38: aload         6
      40: aload         5
      42: invokevirtual #126                // Method java/math/BigDecimal.min:(Ljava/math/BigDecimal;)Ljava/math/BigDecimal;
      45: astore        7
      47: aload         5
      49: invokestatic  #42                 // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
      52: aload         5
      54: aload         7
      56: invokestatic  #42                 // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
      59: aload         7
      61: invokevirtual #65                 // Method java/math/BigDecimal.subtract:(Ljava/math/BigDecimal;)Ljava/math/BigDecimal;
      64: dup
      65: ldc           #67                 // String subtract(...)
      67: invokestatic  #24                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
      70: astore        8
      72: aload         4
      74: astore        9
      76: aload_3
      77: astore        10
      79: aload         4
      81: aload_3
      82: invokevirtual #34                 // Method java/math/BigDecimal.compareTo:(Ljava/math/BigDecimal;)I
      85: ifle          106
      88: aload         10
      90: aload         7
      92: invokevirtual #46                 // Method java/math/BigDecimal.add:(Ljava/math/BigDecimal;)Ljava/math/BigDecimal;
      95: dup
      96: ldc           #48                 // String add(...)
      98: invokestatic  #24                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
     101: astore        10
     103: goto          130
     106: aload_3
     107: aload         4
     109: invokevirtual #34                 // Method java/math/BigDecimal.compareTo:(Ljava/math/BigDecimal;)I
     112: ifle          130
     115: aload         9
     117: aload         7
     119: invokevirtual #46                 // Method java/math/BigDecimal.add:(Ljava/math/BigDecimal;)Ljava/math/BigDecimal;
     122: dup
     123: ldc           #48                 // String add(...)
     125: invokestatic  #24                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
     128: astore        9
     130: aload         9
     132: aload         10
     134: invokestatic  #130                // Method kotlin/jvm/internal/Intrinsics.areEqual:(Ljava/lang/Object;Ljava/lang/Object;)Z
     137: ifeq          210
     140: aload         9
     142: astore        11
     144: aload         8
     146: getstatic     #133                // Field TWO:Ljava/math/BigDecimal;
     149: getstatic     #120                // Field java/math/RoundingMode.FLOOR:Ljava/math/RoundingMode;
     152: invokevirtual #77                 // Method java/math/BigDecimal.divide:(Ljava/math/BigDecimal;Ljava/math/RoundingMode;)Ljava/math/BigDecimal;
     155: dup
     156: ldc           #135                // String divide(...)
     158: invokestatic  #24                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
     161: aload         11
     163: swap
     164: invokevirtual #46                 // Method java/math/BigDecimal.add:(Ljava/math/BigDecimal;)Ljava/math/BigDecimal;
     167: dup
     168: ldc           #48                 // String add(...)
     170: invokestatic  #24                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
     173: astore        9
     175: aload         10
     177: astore        11
     179: aload         8
     181: getstatic     #133                // Field TWO:Ljava/math/BigDecimal;
     184: getstatic     #73                 // Field java/math/RoundingMode.CEILING:Ljava/math/RoundingMode;
     187: invokevirtual #77                 // Method java/math/BigDecimal.divide:(Ljava/math/BigDecimal;Ljava/math/RoundingMode;)Ljava/math/BigDecimal;
     190: dup
     191: ldc           #135                // String divide(...)
     193: invokestatic  #24                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
     196: aload         11
     198: swap
     199: invokevirtual #46                 // Method java/math/BigDecimal.add:(Ljava/math/BigDecimal;)Ljava/math/BigDecimal;
     202: dup
     203: ldc           #48                 // String add(...)
     205: invokestatic  #24                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
     208: astore        10
     210: nop
     211: nop
     212: aload         9
     214: aload         4
     216: invokevirtual #65                 // Method java/math/BigDecimal.subtract:(Ljava/math/BigDecimal;)Ljava/math/BigDecimal;
     219: dup
     220: ldc           #67                 // String subtract(...)
     222: invokestatic  #24                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
     225: aload         10
     227: aload_3
     228: invokevirtual #65                 // Method java/math/BigDecimal.subtract:(Ljava/math/BigDecimal;)Ljava/math/BigDecimal;
     231: dup
     232: ldc           #67                 // String subtract(...)
     234: invokestatic  #24                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
     237: invokevirtual #46                 // Method java/math/BigDecimal.add:(Ljava/math/BigDecimal;)Ljava/math/BigDecimal;
     240: dup
     241: ldc           #48                 // String add(...)
     243: invokestatic  #24                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
     246: aload_2
     247: invokevirtual #84                 // Method java/math/BigDecimal.multiply:(Ljava/math/BigDecimal;)Ljava/math/BigDecimal;
     250: dup
     251: ldc           #86                 // String multiply(...)
     253: invokestatic  #24                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
     256: astore        11
     258: new           #54                 // class ru/amai/study/hackerrank/practice/interviewPreparationKit/search/candies/InvestPlan
     261: dup
     262: new           #26                 // class ru/amai/study/hackerrank/practice/interviewPreparationKit/search/candies/FactoryResources
     265: dup
     266: aload         10
     268: aload         9
     270: invokespecial #30                 // Method ru/amai/study/hackerrank/practice/interviewPreparationKit/search/candies/FactoryResources."<init>":(Ljava/math/BigDecimal;Ljava/math/BigDecimal;)V
     273: aload         11
     275: invokespecial #138                // Method ru/amai/study/hackerrank/practice/interviewPreparationKit/search/candies/InvestPlan."<init>":(Lru/amai/study/hackerrank/practice/interviewPreparationKit/search/candies/FactoryResources;Ljava/math/BigDecimal;)V
     278: areturn

  public static final void main();
    Code:
       0: new           #151                // class java/util/Scanner
       3: dup
       4: getstatic     #157                // Field java/lang/System.in:Ljava/io/InputStream;
       7: invokespecial #160                // Method java/util/Scanner."<init>":(Ljava/io/InputStream;)V
      10: astore_0
      11: aload_0
      12: invokevirtual #164                // Method java/util/Scanner.nextLine:()Ljava/lang/String;
      15: dup
      16: ldc           #166                // String nextLine(...)
      18: invokestatic  #24                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
      21: checkcast     #168                // class java/lang/CharSequence
      24: iconst_1
      25: anewarray     #170                // class java/lang/String
      28: astore_2
      29: aload_2
      30: iconst_0
      31: ldc           #172                // String
      33: aastore
      34: aload_2
      35: iconst_0
      36: iconst_0
      37: bipush        6
      39: aconst_null
      40: invokestatic  #178                // Method kotlin/text/StringsKt.split$default:(Ljava/lang/CharSequence;[Ljava/lang/String;ZIILjava/lang/Object;)Ljava/util/List;
      43: checkcast     #180                // class java/lang/Iterable
      46: astore_2
      47: iconst_0
      48: istore_3
      49: aload_2
      50: astore        4
      52: new           #182                // class java/util/ArrayList
      55: dup
      56: aload_2
      57: bipush        10
      59: invokestatic  #188                // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
      62: invokespecial #191                // Method java/util/ArrayList."<init>":(I)V
      65: checkcast     #193                // class java/util/Collection
      68: astore        5
      70: iconst_0
      71: istore        6
      73: aload         4
      75: invokeinterface #197,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      80: astore        7
      82: aload         7
      84: invokeinterface #203,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
      89: ifeq          146
      92: aload         7
      94: invokeinterface #207,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      99: astore        8
     101: aload         5
     103: aload         8
     105: checkcast     #170                // class java/lang/String
     108: astore        9
     110: astore        12
     112: iconst_0
     113: istore        10
     115: nop
     116: aload         9
     118: checkcast     #168                // class java/lang/CharSequence
     121: invokestatic  #211                // Method kotlin/text/StringsKt.trim:(Ljava/lang/CharSequence;)Ljava/lang/CharSequence;
     124: invokevirtual #214                // Method java/lang/Object.toString:()Ljava/lang/String;
     127: invokestatic  #220                // Method java/lang/Long.parseLong:(Ljava/lang/String;)J
     130: nop
     131: invokestatic  #223                // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
     134: aload         12
     136: swap
     137: invokeinterface #226,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     142: pop
     143: goto          82
     146: aload         5
     148: checkcast     #228                // class java/util/List
     151: nop
     152: astore_1
     153: aload_1
     154: iconst_0
     155: invokeinterface #232,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     160: checkcast     #234                // class java/lang/Number
     163: invokevirtual #235                // Method java/lang/Number.longValue:()J
     166: lstore_2
     167: aload_1
     168: iconst_1
     169: invokeinterface #232,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     174: checkcast     #234                // class java/lang/Number
     177: invokevirtual #235                // Method java/lang/Number.longValue:()J
     180: lstore        4
     182: aload_1
     183: iconst_2
     184: invokeinterface #232,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     189: checkcast     #234                // class java/lang/Number
     192: invokevirtual #235                // Method java/lang/Number.longValue:()J
     195: lstore        6
     197: aload_1
     198: iconst_3
     199: invokeinterface #232,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     204: checkcast     #234                // class java/lang/Number
     207: invokevirtual #235                // Method java/lang/Number.longValue:()J
     210: lstore        8
     212: lload_2
     213: lload         4
     215: lload         6
     217: lload         8
     219: invokestatic  #237                // Method minimumPasses:(JJJJ)J
     222: lstore        10
     224: getstatic     #241                // Field java/lang/System.out:Ljava/io/PrintStream;
     227: lload         10
     229: invokevirtual #247                // Method java/io/PrintStream.println:(J)V
     232: return

  public static void main(java.lang.String[]);
    Code:
       0: invokestatic  #266                // Method main:()V
       3: return

  static {};
    Code:
       0: ldc2_w        #270                // long 2l
       3: invokestatic  #16                 // Method java/math/BigDecimal.valueOf:(J)Ljava/math/BigDecimal;
       6: putstatic     #133                // Field TWO:Ljava/math/BigDecimal;
       9: return
}


package ru.amai.study.hackerrank.practice.interviewPreparationKit.search.swap

import java.util.*

fun swapNodes(indexes: Array<Array<Int>>, queries: Array<Int>): List<List<Int>> {
    val root = buildTree(indexes)

    return queries.map { swapMultiplier ->
        val queryResult = mutableListOf<Int>()
        traverseInOrderWithSwaps(root, swapMultiplier) { node ->
            queryResult += node.index
        }
        queryResult
    }
}

private fun traverseInOrderWithSwaps(
    node: Node,
    swapMultiplier: Int,
    level: Int = 1,
    observer: (Node) -> Unit
) {
    if (level % swapMultiplier == 0) node.swapSubTrees()

    val (_, left, right) = node
    left?.let { traverseInOrderWithSwaps(left, swapMultiplier, level + 1, observer) }
    observer(node)
    right?.let { traverseInOrderWithSwaps(right, swapMultiplier, level + 1, observer) }
}

private fun buildTree(indexes: Array<Array<Int>>): Node {
    val nodeMap = mutableMapOf<Int, Node>()
    indexes.forEachIndexed { i, (l, r) ->
        val p = i + 1
        listOf(p, l, r)
            .filter { it != -1 }
            .forEach { nodeMap.putIfAbsent(it, Node(it)) }

        if (l != -1) nodeMap.getValue(p).left = nodeMap.getValue(l)
        if (r != -1) nodeMap.getValue(p).right = nodeMap.getValue(r)
    }
    return nodeMap.getValue(1)
}

private data class Node(
    val index: Int,
    var left: Node? = null,
    var right: Node? = null
)

private fun Node.swapSubTrees() {
    val tmp = left
    left = right
    right = tmp
}

fun main() {
    val scan = Scanner(System.`in`)

    val n = scan.nextLine().trim().toInt()

    val indexes = Array(n) { Array(2) { 0 } }

    for (indexesRowItr in 0 until n) {
        indexes[indexesRowItr] = scan.nextLine().split(" ").map { it.trim().toInt() }.toTypedArray()
    }

    val queriesCount = scan.nextLine().trim().toInt()

    val queries = Array(queriesCount) { 0 }
    for (queriesItr in 0 until queriesCount) {
        val queriesItem = scan.nextLine().trim().toInt()
        queries[queriesItr] = queriesItem
    }

    val result = swapNodes(indexes, queries)

    println(result.joinToString("\n") { it.joinToString(" ") })
}

Compiled from "SwapNodes.kt"
public final class ru.amai.study.hackerrank.practice.interviewPreparationKit.search.swap.SwapNodesKt {
  public static final java.util.List<java.util.List<java.lang.Integer>> swapNodes(java.lang.Integer[][], java.lang.Integer[]);
    Code:
       0: aload_0
       1: ldc           #10                 // String indexes
       3: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_1
       7: ldc           #18                 // String queries
       9: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      12: aload_0
      13: invokestatic  #22                 // Method buildTree:([[Ljava/lang/Integer;)Lru/amai/study/hackerrank/practice/interviewPreparationKit/search/swap/Node;
      16: astore_2
      17: aload_1
      18: astore_3
      19: iconst_0
      20: istore        4
      22: aload_3
      23: astore        5
      25: new           #24                 // class java/util/ArrayList
      28: dup
      29: aload_3
      30: arraylength
      31: invokespecial #28                 // Method java/util/ArrayList."<init>":(I)V
      34: checkcast     #30                 // class java/util/Collection
      37: astore        6
      39: iconst_0
      40: istore        7
      42: iconst_0
      43: istore        8
      45: aload         5
      47: arraylength
      48: istore        9
      50: iload         8
      52: iload         9
      54: if_icmpge     126
      57: aload         5
      59: iload         8
      61: aaload
      62: astore        10
      64: aload         6
      66: aload         10
      68: checkcast     #32                 // class java/lang/Number
      71: invokevirtual #36                 // Method java/lang/Number.intValue:()I
      74: istore        11
      76: astore        14
      78: iconst_0
      79: istore        12
      81: new           #24                 // class java/util/ArrayList
      84: dup
      85: invokespecial #39                 // Method java/util/ArrayList."<init>":()V
      88: checkcast     #41                 // class java/util/List
      91: astore        13
      93: aload_2
      94: iload         11
      96: iconst_0
      97: aload         13
      99: invokedynamic #61,  0             // InvokeDynamic #0:invoke:(Ljava/util/List;)Lkotlin/jvm/functions/Function1;
     104: iconst_4
     105: aconst_null
     106: invokestatic  #65                 // Method traverseInOrderWithSwaps$default:(Lru/amai/study/hackerrank/practice/interviewPreparationKit/search/swap/Node;IILkotlin/jvm/functions/Function1;ILjava/lang/Object;)V
     109: aload         13
     111: aload         14
     113: swap
     114: invokeinterface #69,  2           // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     119: pop
     120: iinc          8, 1
     123: goto          50
     126: aload         6
     128: checkcast     #41                 // class java/util/List
     131: nop
     132: areturn

  private static final void traverseInOrderWithSwaps(ru.amai.study.hackerrank.practice.interviewPreparationKit.search.swap.Node, int, int, kotlin.jvm.functions.Function1<? super ru.amai.study.hackerrank.practice.interviewPreparationKit.search.swap.Node, kotlin.Unit>);
    Code:
       0: iload_2
       1: iload_1
       2: irem
       3: ifne          10
       6: aload_0
       7: invokestatic  #98                 // Method swapSubTrees:(Lru/amai/study/hackerrank/practice/interviewPreparationKit/search/swap/Node;)V
      10: aload_0
      11: invokevirtual #102                // Method ru/amai/study/hackerrank/practice/interviewPreparationKit/search/swap/Node.component2:()Lru/amai/study/hackerrank/practice/interviewPreparationKit/search/swap/Node;
      14: astore        4
      16: aload_0
      17: invokevirtual #105                // Method ru/amai/study/hackerrank/practice/interviewPreparationKit/search/swap/Node.component3:()Lru/amai/study/hackerrank/practice/interviewPreparationKit/search/swap/Node;
      20: astore        5
      22: aload         4
      24: dup
      25: ifnull        46
      28: astore        6
      30: iconst_0
      31: istore        7
      33: aload         4
      35: iload_1
      36: iload_2
      37: iconst_1
      38: iadd
      39: aload_3
      40: invokestatic  #107                // Method traverseInOrderWithSwaps:(Lru/amai/study/hackerrank/practice/interviewPreparationKit/search/swap/Node;IILkotlin/jvm/functions/Function1;)V
      43: goto          48
      46: pop
      47: nop
      48: aload_3
      49: aload_0
      50: invokeinterface #111,  2          // InterfaceMethod kotlin/jvm/functions/Function1.invoke:(Ljava/lang/Object;)Ljava/lang/Object;
      55: pop
      56: aload         5
      58: dup
      59: ifnull        80
      62: astore        6
      64: iconst_0
      65: istore        7
      67: aload         5
      69: iload_1
      70: iload_2
      71: iconst_1
      72: iadd
      73: aload_3
      74: invokestatic  #107                // Method traverseInOrderWithSwaps:(Lru/amai/study/hackerrank/practice/interviewPreparationKit/search/swap/Node;IILkotlin/jvm/functions/Function1;)V
      77: goto          82
      80: pop
      81: nop
      82: return

  static void traverseInOrderWithSwaps$default(ru.amai.study.hackerrank.practice.interviewPreparationKit.search.swap.Node, int, int, kotlin.jvm.functions.Function1, int, java.lang.Object);
    Code:
       0: iload         4
       2: iconst_4
       3: iand
       4: ifeq          9
       7: iconst_1
       8: istore_2
       9: aload_0
      10: iload_1
      11: iload_2
      12: aload_3
      13: invokestatic  #107                // Method traverseInOrderWithSwaps:(Lru/amai/study/hackerrank/practice/interviewPreparationKit/search/swap/Node;IILkotlin/jvm/functions/Function1;)V
      16: return

  private static final ru.amai.study.hackerrank.practice.interviewPreparationKit.search.swap.Node buildTree(java.lang.Integer[][]);
    Code:
       0: new           #122                // class java/util/LinkedHashMap
       3: dup
       4: invokespecial #123                // Method java/util/LinkedHashMap."<init>":()V
       7: checkcast     #125                // class java/util/Map
      10: astore_1
      11: aload_0
      12: checkcast     #126                // class "[Ljava/lang/Object;"
      15: astore_2
      16: iconst_0
      17: istore_3
      18: iconst_0
      19: istore        4
      21: iconst_0
      22: istore        5
      24: aload_2
      25: arraylength
      26: istore        6
      28: iload         5
      30: iload         6
      32: if_icmpge     383
      35: aload_2
      36: iload         5
      38: aaload
      39: astore        7
      41: iload         4
      43: iinc          4, 1
      46: aload         7
      48: checkcast     #89                 // class "[Ljava/lang/Integer;"
      51: astore        8
      53: istore        9
      55: iconst_0
      56: istore        10
      58: aload         8
      60: iconst_0
      61: aaload
      62: checkcast     #32                 // class java/lang/Number
      65: invokevirtual #36                 // Method java/lang/Number.intValue:()I
      68: istore        11
      70: aload         8
      72: iconst_1
      73: aaload
      74: checkcast     #32                 // class java/lang/Number
      77: invokevirtual #36                 // Method java/lang/Number.intValue:()I
      80: istore        12
      82: iload         9
      84: iconst_1
      85: iadd
      86: istore        13
      88: iconst_3
      89: anewarray     #128                // class java/lang/Integer
      92: astore        14
      94: aload         14
      96: iconst_0
      97: iload         13
      99: invokestatic  #132                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     102: aastore
     103: aload         14
     105: iconst_1
     106: iload         11
     108: invokestatic  #132                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     111: aastore
     112: aload         14
     114: iconst_2
     115: iload         12
     117: invokestatic  #132                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     120: aastore
     121: aload         14
     123: invokestatic  #138                // Method kotlin/collections/CollectionsKt.listOf:([Ljava/lang/Object;)Ljava/util/List;
     126: checkcast     #140                // class java/lang/Iterable
     129: astore        14
     131: nop
     132: iconst_0
     133: istore        15
     135: aload         14
     137: astore        16
     139: new           #24                 // class java/util/ArrayList
     142: dup
     143: invokespecial #39                 // Method java/util/ArrayList."<init>":()V
     146: checkcast     #30                 // class java/util/Collection
     149: astore        17
     151: iconst_0
     152: istore        18
     154: aload         16
     156: invokeinterface #144,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     161: astore        19
     163: aload         19
     165: invokeinterface #150,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
     170: ifeq          222
     173: aload         19
     175: invokeinterface #154,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     180: astore        20
     182: aload         20
     184: checkcast     #32                 // class java/lang/Number
     187: invokevirtual #36                 // Method java/lang/Number.intValue:()I
     190: istore        21
     192: iconst_0
     193: istore        22
     195: iload         21
     197: iconst_m1
     198: if_icmpeq     205
     201: iconst_1
     202: goto          206
     205: iconst_0
     206: ifeq          163
     209: aload         17
     211: aload         20
     213: invokeinterface #69,  2           // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     218: pop
     219: goto          163
     222: aload         17
     224: checkcast     #41                 // class java/util/List
     227: nop
     228: checkcast     #140                // class java/lang/Iterable
     231: astore        14
     233: nop
     234: iconst_0
     235: istore        15
     237: aload         14
     239: invokeinterface #144,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     244: astore        16
     246: aload         16
     248: invokeinterface #150,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
     253: ifeq          308
     256: aload         16
     258: invokeinterface #154,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     263: astore        17
     265: aload         17
     267: checkcast     #32                 // class java/lang/Number
     270: invokevirtual #36                 // Method java/lang/Number.intValue:()I
     273: istore        18
     275: iconst_0
     276: istore        19
     278: aload_1
     279: iload         18
     281: invokestatic  #132                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     284: new           #91                 // class ru/amai/study/hackerrank/practice/interviewPreparationKit/search/swap/Node
     287: dup
     288: iload         18
     290: aconst_null
     291: aconst_null
     292: bipush        6
     294: aconst_null
     295: invokespecial #157                // Method ru/amai/study/hackerrank/practice/interviewPreparationKit/search/swap/Node."<init>":(ILru/amai/study/hackerrank/practice/interviewPreparationKit/search/swap/Node;Lru/amai/study/hackerrank/practice/interviewPreparationKit/search/swap/Node;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
     298: invokeinterface #161,  3          // InterfaceMethod java/util/Map.putIfAbsent:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
     303: pop
     304: nop
     305: goto          246
     308: nop
     309: iload         11
     311: iconst_m1
     312: if_icmpeq     342
     315: aload_1
     316: iload         13
     318: invokestatic  #132                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     321: invokestatic  #167                // Method kotlin/collections/MapsKt.getValue:(Ljava/util/Map;Ljava/lang/Object;)Ljava/lang/Object;
     324: checkcast     #91                 // class ru/amai/study/hackerrank/practice/interviewPreparationKit/search/swap/Node
     327: aload_1
     328: iload         11
     330: invokestatic  #132                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     333: invokestatic  #167                // Method kotlin/collections/MapsKt.getValue:(Ljava/util/Map;Ljava/lang/Object;)Ljava/lang/Object;
     336: checkcast     #91                 // class ru/amai/study/hackerrank/practice/interviewPreparationKit/search/swap/Node
     339: invokevirtual #170                // Method ru/amai/study/hackerrank/practice/interviewPreparationKit/search/swap/Node.setLeft:(Lru/amai/study/hackerrank/practice/interviewPreparationKit/search/swap/Node;)V
     342: iload         12
     344: iconst_m1
     345: if_icmpeq     375
     348: aload_1
     349: iload         13
     351: invokestatic  #132                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     354: invokestatic  #167                // Method kotlin/collections/MapsKt.getValue:(Ljava/util/Map;Ljava/lang/Object;)Ljava/lang/Object;
     357: checkcast     #91                 // class ru/amai/study/hackerrank/practice/interviewPreparationKit/search/swap/Node
     360: aload_1
     361: iload         12
     363: invokestatic  #132                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     366: invokestatic  #167                // Method kotlin/collections/MapsKt.getValue:(Ljava/util/Map;Ljava/lang/Object;)Ljava/lang/Object;
     369: checkcast     #91                 // class ru/amai/study/hackerrank/practice/interviewPreparationKit/search/swap/Node
     372: invokevirtual #173                // Method ru/amai/study/hackerrank/practice/interviewPreparationKit/search/swap/Node.setRight:(Lru/amai/study/hackerrank/practice/interviewPreparationKit/search/swap/Node;)V
     375: nop
     376: nop
     377: iinc          5, 1
     380: goto          28
     383: nop
     384: aload_1
     385: iconst_1
     386: invokestatic  #132                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     389: invokestatic  #167                // Method kotlin/collections/MapsKt.getValue:(Ljava/util/Map;Ljava/lang/Object;)Ljava/lang/Object;
     392: checkcast     #91                 // class ru/amai/study/hackerrank/practice/interviewPreparationKit/search/swap/Node
     395: areturn

  private static final void swapSubTrees(ru.amai.study.hackerrank.practice.interviewPreparationKit.search.swap.Node);
    Code:
       0: aload_0
       1: invokevirtual #198                // Method ru/amai/study/hackerrank/practice/interviewPreparationKit/search/swap/Node.getLeft:()Lru/amai/study/hackerrank/practice/interviewPreparationKit/search/swap/Node;
       4: astore_1
       5: aload_0
       6: aload_0
       7: invokevirtual #201                // Method ru/amai/study/hackerrank/practice/interviewPreparationKit/search/swap/Node.getRight:()Lru/amai/study/hackerrank/practice/interviewPreparationKit/search/swap/Node;
      10: invokevirtual #170                // Method ru/amai/study/hackerrank/practice/interviewPreparationKit/search/swap/Node.setLeft:(Lru/amai/study/hackerrank/practice/interviewPreparationKit/search/swap/Node;)V
      13: aload_0
      14: aload_1
      15: invokevirtual #173                // Method ru/amai/study/hackerrank/practice/interviewPreparationKit/search/swap/Node.setRight:(Lru/amai/study/hackerrank/practice/interviewPreparationKit/search/swap/Node;)V
      18: return

  public static final void main();
    Code:
       0: new           #206                // class java/util/Scanner
       3: dup
       4: getstatic     #212                // Field java/lang/System.in:Ljava/io/InputStream;
       7: invokespecial #215                // Method java/util/Scanner."<init>":(Ljava/io/InputStream;)V
      10: astore_0
      11: aload_0
      12: invokevirtual #219                // Method java/util/Scanner.nextLine:()Ljava/lang/String;
      15: dup
      16: ldc           #221                // String nextLine(...)
      18: invokestatic  #224                // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
      21: checkcast     #226                // class java/lang/CharSequence
      24: invokestatic  #232                // Method kotlin/text/StringsKt.trim:(Ljava/lang/CharSequence;)Ljava/lang/CharSequence;
      27: invokevirtual #235                // Method java/lang/Object.toString:()Ljava/lang/String;
      30: invokestatic  #239                // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
      33: istore_1
      34: iconst_0
      35: istore_3
      36: iload_1
      37: anewarray     #89                 // class "[Ljava/lang/Integer;"
      40: astore        4
      42: iload_3
      43: iload_1
      44: if_icmpge     105
      47: iload_3
      48: istore        5
      50: aload         4
      52: iload         5
      54: iconst_0
      55: istore        6
      57: iconst_2
      58: anewarray     #128                // class java/lang/Integer
      61: astore        7
      63: istore        14
      65: astore        13
      67: iload         6
      69: iconst_2
      70: if_icmpge     92
      73: iload         6
      75: istore        8
      77: aload         7
      79: iload         8
      81: iconst_0
      82: invokestatic  #132                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      85: aastore
      86: iinc          6, 1
      89: goto          67
      92: aload         13
      94: iload         14
      96: aload         7
      98: aastore
      99: iinc          3, 1
     102: goto          42
     105: aload         4
     107: astore_2
     108: iconst_0
     109: istore_3
     110: iload_3
     111: iload_1
     112: if_icmpge     308
     115: aload_2
     116: iload_3
     117: aload_0
     118: invokevirtual #219                // Method java/util/Scanner.nextLine:()Ljava/lang/String;
     121: dup
     122: ldc           #221                // String nextLine(...)
     124: invokestatic  #224                // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
     127: checkcast     #226                // class java/lang/CharSequence
     130: iconst_1
     131: anewarray     #241                // class java/lang/String
     134: astore        4
     136: aload         4
     138: iconst_0
     139: ldc           #243                // String
     141: aastore
     142: aload         4
     144: iconst_0
     145: iconst_0
     146: bipush        6
     148: aconst_null
     149: invokestatic  #247                // Method kotlin/text/StringsKt.split$default:(Ljava/lang/CharSequence;[Ljava/lang/String;ZIILjava/lang/Object;)Ljava/util/List;
     152: checkcast     #140                // class java/lang/Iterable
     155: astore        4
     157: istore        14
     159: astore        13
     161: iconst_0
     162: istore        5
     164: aload         4
     166: astore        6
     168: new           #24                 // class java/util/ArrayList
     171: dup
     172: aload         4
     174: bipush        10
     176: invokestatic  #251                // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
     179: invokespecial #28                 // Method java/util/ArrayList."<init>":(I)V
     182: checkcast     #30                 // class java/util/Collection
     185: astore        7
     187: iconst_0
     188: istore        8
     190: aload         6
     192: invokeinterface #144,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     197: astore        9
     199: aload         9
     201: invokeinterface #150,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
     206: ifeq          263
     209: aload         9
     211: invokeinterface #154,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     216: astore        10
     218: aload         7
     220: aload         10
     222: checkcast     #241                // class java/lang/String
     225: astore        11
     227: astore        15
     229: iconst_0
     230: istore        12
     232: nop
     233: aload         11
     235: checkcast     #226                // class java/lang/CharSequence
     238: invokestatic  #232                // Method kotlin/text/StringsKt.trim:(Ljava/lang/CharSequence;)Ljava/lang/CharSequence;
     241: invokevirtual #235                // Method java/lang/Object.toString:()Ljava/lang/String;
     244: invokestatic  #239                // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
     247: nop
     248: invokestatic  #132                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     251: aload         15
     253: swap
     254: invokeinterface #69,  2           // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     259: pop
     260: goto          199
     263: aload         7
     265: checkcast     #41                 // class java/util/List
     268: nop
     269: astore        15
     271: aload         13
     273: iload         14
     275: aload         15
     277: checkcast     #30                 // class java/util/Collection
     280: astore        4
     282: nop
     283: iconst_0
     284: istore        5
     286: aload         4
     288: astore        6
     290: aload         6
     292: iconst_0
     293: anewarray     #128                // class java/lang/Integer
     296: invokeinterface #255,  2          // InterfaceMethod java/util/Collection.toArray:([Ljava/lang/Object;)[Ljava/lang/Object;
     301: aastore
     302: iinc          3, 1
     305: goto          110
     308: aload_0
     309: invokevirtual #219                // Method java/util/Scanner.nextLine:()Ljava/lang/String;
     312: dup
     313: ldc           #221                // String nextLine(...)
     315: invokestatic  #224                // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
     318: checkcast     #226                // class java/lang/CharSequence
     321: invokestatic  #232                // Method kotlin/text/StringsKt.trim:(Ljava/lang/CharSequence;)Ljava/lang/CharSequence;
     324: invokevirtual #235                // Method java/lang/Object.toString:()Ljava/lang/String;
     327: invokestatic  #239                // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
     330: istore_3
     331: iconst_0
     332: istore        5
     334: iload_3
     335: anewarray     #128                // class java/lang/Integer
     338: astore        6
     340: iload         5
     342: iload_3
     343: if_icmpge     365
     346: iload         5
     348: istore        7
     350: aload         6
     352: iload         7
     354: iconst_0
     355: invokestatic  #132                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     358: aastore
     359: iinc          5, 1
     362: goto          340
     365: aload         6
     367: astore        4
     369: iconst_0
     370: istore        5
     372: iload         5
     374: iload_3
     375: if_icmpge     418
     378: aload_0
     379: invokevirtual #219                // Method java/util/Scanner.nextLine:()Ljava/lang/String;
     382: dup
     383: ldc           #221                // String nextLine(...)
     385: invokestatic  #224                // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
     388: checkcast     #226                // class java/lang/CharSequence
     391: invokestatic  #232                // Method kotlin/text/StringsKt.trim:(Ljava/lang/CharSequence;)Ljava/lang/CharSequence;
     394: invokevirtual #235                // Method java/lang/Object.toString:()Ljava/lang/String;
     397: invokestatic  #239                // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
     400: istore        6
     402: aload         4
     404: iload         5
     406: iload         6
     408: invokestatic  #132                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     411: aastore
     412: iinc          5, 1
     415: goto          372
     418: aload_2
     419: aload         4
     421: invokestatic  #257                // Method swapNodes:([[Ljava/lang/Integer;[Ljava/lang/Integer;)Ljava/util/List;
     424: astore        5
     426: aload         5
     428: checkcast     #140                // class java/lang/Iterable
     431: ldc_w         #259                // String \n
     434: checkcast     #226                // class java/lang/CharSequence
     437: aconst_null
     438: aconst_null
     439: iconst_0
     440: aconst_null
     441: invokedynamic #268,  0            // InvokeDynamic #1:invoke:()Lkotlin/jvm/functions/Function1;
     446: bipush        30
     448: aconst_null
     449: invokestatic  #272                // Method kotlin/collections/CollectionsKt.joinToString$default:(Ljava/lang/Iterable;Ljava/lang/CharSequence;Ljava/lang/CharSequence;Ljava/lang/CharSequence;ILjava/lang/CharSequence;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Ljava/lang/String;
     452: getstatic     #276                // Field java/lang/System.out:Ljava/io/PrintStream;
     455: swap
     456: invokevirtual #282                // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     459: return

  public static void main(java.lang.String[]);
    Code:
       0: invokestatic  #298                // Method main:()V
       3: return

  private static final kotlin.Unit swapNodes$lambda$1$lambda$0(java.util.List, ru.amai.study.hackerrank.practice.interviewPreparationKit.search.swap.Node);
    Code:
       0: aload_1
       1: ldc_w         #301                // String node
       4: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       7: aload_0
       8: checkcast     #30                 // class java/util/Collection
      11: aload_1
      12: invokevirtual #304                // Method ru/amai/study/hackerrank/practice/interviewPreparationKit/search/swap/Node.getIndex:()I
      15: invokestatic  #132                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      18: invokeinterface #69,  2           // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
      23: pop
      24: getstatic     #310                // Field kotlin/Unit.INSTANCE:Lkotlin/Unit;
      27: areturn

  private static final java.lang.CharSequence main$lambda$8(java.util.List);
    Code:
       0: aload_0
       1: ldc_w         #312                // String it
       4: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       7: aload_0
       8: checkcast     #140                // class java/lang/Iterable
      11: ldc           #243                // String
      13: checkcast     #226                // class java/lang/CharSequence
      16: aconst_null
      17: aconst_null
      18: iconst_0
      19: aconst_null
      20: aconst_null
      21: bipush        62
      23: aconst_null
      24: invokestatic  #272                // Method kotlin/collections/CollectionsKt.joinToString$default:(Ljava/lang/Iterable;Ljava/lang/CharSequence;Ljava/lang/CharSequence;Ljava/lang/CharSequence;ILjava/lang/CharSequence;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Ljava/lang/String;
      27: checkcast     #226                // class java/lang/CharSequence
      30: areturn
}


package ru.amai.study.hackerrank.practice.interviewPreparationKit.search.triplets

import java.util.*

fun triplets(a: IntArray, b: IntArray, c: IntArray): Long {

    val (aSorted, aSize) = sortUniqueToSize(a)
    val (bSorted, bSize) = sortUniqueToSize(b)
    val (cSorted, cSize) = sortUniqueToSize(c)

    val totalSize = aSize + bSize + cSize

    var aIndex = 0
    var bIndex = 0
    var cIndex = 0

    var tripletsCnt = 0L

    while ((aIndex + bIndex + cIndex) < totalSize) {

        if (bIndex == bSize) break

        val p = aSorted.getOrMaxValue(aIndex)
        val q = bSorted.getOrMaxValue(bIndex)
        val r = cSorted.getOrMaxValue(cIndex)

        when {
            (p <= q) && (p <= r) -> aIndex++
            (r <= q) && (r <= p) -> cIndex++
            else -> {
                bIndex++
                tripletsCnt += aIndex.toLong() * cIndex.toLong()
            }
        }
    }

    return tripletsCnt
}

private fun IntArray.getOrMaxValue(index: Int) =
    if (index < size) get(index) else Int.MAX_VALUE

private fun sortUniqueToSize(array: IntArray): Pair<IntArray, Int> {
    val sortedUnique = array.sorted().distinct().toIntArray()
    return sortedUnique to sortedUnique.size
}

fun main() {
    val scan = Scanner(System.`in`)

    scan.nextLine().split(" ")

    fun readIntArray(): IntArray =
        scan.nextLine().split(" ").map { it.trim().toInt() }.toIntArray()

    val a = readIntArray()
    val b = readIntArray()
    val c = readIntArray()

    val ans = triplets(a, b, c)
    println(ans)
}

Compiled from "Triplets.kt"
public final class ru.amai.study.hackerrank.practice.interviewPreparationKit.search.triplets.TripletsKt {
  public static final long triplets(int[], int[], int[]);
    Code:
       0: aload_0
       1: ldc           #9                  // String a
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_1
       7: ldc           #17                 // String b
       9: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      12: aload_2
      13: ldc           #19                 // String c
      15: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      18: aload_0
      19: invokestatic  #23                 // Method sortUniqueToSize:([I)Lkotlin/Pair;
      22: astore_3
      23: aload_3
      24: invokevirtual #29                 // Method kotlin/Pair.component1:()Ljava/lang/Object;
      27: checkcast     #31                 // class "[I"
      30: astore        4
      32: aload_3
      33: invokevirtual #34                 // Method kotlin/Pair.component2:()Ljava/lang/Object;
      36: checkcast     #36                 // class java/lang/Number
      39: invokevirtual #40                 // Method java/lang/Number.intValue:()I
      42: istore        5
      44: aload_1
      45: invokestatic  #23                 // Method sortUniqueToSize:([I)Lkotlin/Pair;
      48: astore        6
      50: aload         6
      52: invokevirtual #29                 // Method kotlin/Pair.component1:()Ljava/lang/Object;
      55: checkcast     #31                 // class "[I"
      58: astore        7
      60: aload         6
      62: invokevirtual #34                 // Method kotlin/Pair.component2:()Ljava/lang/Object;
      65: checkcast     #36                 // class java/lang/Number
      68: invokevirtual #40                 // Method java/lang/Number.intValue:()I
      71: istore        8
      73: aload_2
      74: invokestatic  #23                 // Method sortUniqueToSize:([I)Lkotlin/Pair;
      77: astore        9
      79: aload         9
      81: invokevirtual #29                 // Method kotlin/Pair.component1:()Ljava/lang/Object;
      84: checkcast     #31                 // class "[I"
      87: astore        10
      89: aload         9
      91: invokevirtual #34                 // Method kotlin/Pair.component2:()Ljava/lang/Object;
      94: checkcast     #36                 // class java/lang/Number
      97: invokevirtual #40                 // Method java/lang/Number.intValue:()I
     100: istore        11
     102: iload         5
     104: iload         8
     106: iadd
     107: iload         11
     109: iadd
     110: istore        12
     112: iconst_0
     113: istore        13
     115: iconst_0
     116: istore        14
     118: iconst_0
     119: istore        15
     121: lconst_0
     122: lstore        16
     124: iload         13
     126: iload         14
     128: iadd
     129: iload         15
     131: iadd
     132: iload         12
     134: if_icmpge     236
     137: iload         14
     139: iload         8
     141: if_icmpeq     236
     144: aload         4
     146: iload         13
     148: invokestatic  #44                 // Method getOrMaxValue:([II)I
     151: istore        18
     153: aload         7
     155: iload         14
     157: invokestatic  #44                 // Method getOrMaxValue:([II)I
     160: istore        19
     162: aload         10
     164: iload         15
     166: invokestatic  #44                 // Method getOrMaxValue:([II)I
     169: istore        20
     171: nop
     172: iload         18
     174: iload         19
     176: if_icmpgt     195
     179: iload         18
     181: iload         20
     183: if_icmpgt     195
     186: iload         13
     188: iinc          13, 1
     191: pop
     192: goto          124
     195: iload         20
     197: iload         19
     199: if_icmpgt     218
     202: iload         20
     204: iload         18
     206: if_icmpgt     218
     209: iload         15
     211: iinc          15, 1
     214: pop
     215: goto          124
     218: iinc          14, 1
     221: lload         16
     223: iload         13
     225: i2l
     226: iload         15
     228: i2l
     229: lmul
     230: ladd
     231: lstore        16
     233: goto          124
     236: lload         16
     238: lreturn

  private static final int getOrMaxValue(int[], int);
    Code:
       0: iload_1
       1: aload_0
       2: arraylength
       3: if_icmpge     12
       6: aload_0
       7: iload_1
       8: iaload
       9: goto          14
      12: ldc           #61                 // int 2147483647
      14: ireturn

  private static final kotlin.Pair<int[], java.lang.Integer> sortUniqueToSize(int[]);
    Code:
       0: aload_0
       1: invokestatic  #70                 // Method kotlin/collections/ArraysKt.sorted:([I)Ljava/util/List;
       4: checkcast     #72                 // class java/lang/Iterable
       7: invokestatic  #78                 // Method kotlin/collections/CollectionsKt.distinct:(Ljava/lang/Iterable;)Ljava/util/List;
      10: checkcast     #80                 // class java/util/Collection
      13: invokestatic  #84                 // Method kotlin/collections/CollectionsKt.toIntArray:(Ljava/util/Collection;)[I
      16: astore_1
      17: aload_1
      18: aload_1
      19: arraylength
      20: invokestatic  #90                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      23: invokestatic  #96                 // Method kotlin/TuplesKt.to:(Ljava/lang/Object;Ljava/lang/Object;)Lkotlin/Pair;
      26: areturn

  public static final void main();
    Code:
       0: new           #102                // class java/util/Scanner
       3: dup
       4: getstatic     #108                // Field java/lang/System.in:Ljava/io/InputStream;
       7: invokespecial #112                // Method java/util/Scanner."<init>":(Ljava/io/InputStream;)V
      10: astore_0
      11: aload_0
      12: invokevirtual #116                // Method java/util/Scanner.nextLine:()Ljava/lang/String;
      15: dup
      16: ldc           #118                // String nextLine(...)
      18: invokestatic  #121                // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
      21: checkcast     #123                // class java/lang/CharSequence
      24: iconst_1
      25: anewarray     #125                // class java/lang/String
      28: astore_1
      29: aload_1
      30: iconst_0
      31: ldc           #127                // String
      33: aastore
      34: aload_1
      35: iconst_0
      36: iconst_0
      37: bipush        6
      39: aconst_null
      40: invokestatic  #133                // Method kotlin/text/StringsKt.split$default:(Ljava/lang/CharSequence;[Ljava/lang/String;ZIILjava/lang/Object;)Ljava/util/List;
      43: pop
      44: aload_0
      45: invokestatic  #137                // Method main$readIntArray:(Ljava/util/Scanner;)[I
      48: astore_1
      49: aload_0
      50: invokestatic  #137                // Method main$readIntArray:(Ljava/util/Scanner;)[I
      53: astore_2
      54: aload_0
      55: invokestatic  #137                // Method main$readIntArray:(Ljava/util/Scanner;)[I
      58: astore_3
      59: aload_1
      60: aload_2
      61: aload_3
      62: invokestatic  #139                // Method triplets:([I[I[I)J
      65: lstore        4
      67: getstatic     #143                // Field java/lang/System.out:Ljava/io/PrintStream;
      70: lload         4
      72: invokevirtual #149                // Method java/io/PrintStream.println:(J)V
      75: return

  public static void main(java.lang.String[]);
    Code:
       0: invokestatic  #155                // Method main:()V
       3: return

  private static final int[] main$readIntArray(java.util.Scanner);
    Code:
       0: aload_0
       1: invokevirtual #116                // Method java/util/Scanner.nextLine:()Ljava/lang/String;
       4: dup
       5: ldc           #118                // String nextLine(...)
       7: invokestatic  #121                // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
      10: checkcast     #123                // class java/lang/CharSequence
      13: iconst_1
      14: anewarray     #125                // class java/lang/String
      17: astore_1
      18: aload_1
      19: iconst_0
      20: ldc           #127                // String
      22: aastore
      23: aload_1
      24: iconst_0
      25: iconst_0
      26: bipush        6
      28: aconst_null
      29: invokestatic  #133                // Method kotlin/text/StringsKt.split$default:(Ljava/lang/CharSequence;[Ljava/lang/String;ZIILjava/lang/Object;)Ljava/util/List;
      32: checkcast     #72                 // class java/lang/Iterable
      35: astore_1
      36: iconst_0
      37: istore_2
      38: aload_1
      39: astore_3
      40: new           #159                // class java/util/ArrayList
      43: dup
      44: aload_1
      45: bipush        10
      47: invokestatic  #163                // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
      50: invokespecial #166                // Method java/util/ArrayList."<init>":(I)V
      53: checkcast     #80                 // class java/util/Collection
      56: astore        4
      58: iconst_0
      59: istore        5
      61: aload_3
      62: invokeinterface #170,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      67: astore        6
      69: aload         6
      71: invokeinterface #176,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
      76: ifeq          133
      79: aload         6
      81: invokeinterface #179,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      86: astore        7
      88: aload         4
      90: aload         7
      92: checkcast     #125                // class java/lang/String
      95: astore        8
      97: astore        10
      99: iconst_0
     100: istore        9
     102: nop
     103: aload         8
     105: checkcast     #123                // class java/lang/CharSequence
     108: invokestatic  #183                // Method kotlin/text/StringsKt.trim:(Ljava/lang/CharSequence;)Ljava/lang/CharSequence;
     111: invokevirtual #186                // Method java/lang/Object.toString:()Ljava/lang/String;
     114: invokestatic  #190                // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
     117: nop
     118: invokestatic  #90                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     121: aload         10
     123: swap
     124: invokeinterface #194,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     129: pop
     130: goto          69
     133: aload         4
     135: checkcast     #196                // class java/util/List
     138: nop
     139: checkcast     #80                 // class java/util/Collection
     142: invokestatic  #84                 // Method kotlin/collections/CollectionsKt.toIntArray:(Ljava/util/Collection;)[I
     145: areturn
}


package ru.amai.study.hackerrank.practice.interviewPreparationKit.search.minTime

import java.util.*
import kotlin.math.ceil
import kotlin.math.floor

fun minTime(machines: List<Int>, goal: Int): Long {
    fun producedByMachinesIn(time: Long): Long =
        machines.fold(0L) { total, machine -> total + time / machine }

    val approxIndividualGoal = goal.toDouble() / machines.size
    val lowerBound: Long = floor(approxIndividualGoal).toLong() * machines.min()!!
    val upperBound: Long = ceil(approxIndividualGoal).toLong() * machines.max()!!

    fun search(left: Long, right: Long): Long {
        check(right > left) { "right ($right) should be > left ($left)" }

        val mTime = (left + right) / 2
        val mQuantity = producedByMachinesIn(mTime)
        return when {
            mQuantity < goal -> search(mTime + 1, right)
            (mQuantity >= goal) && (producedByMachinesIn(mTime - 1) < goal) -> mTime
            else -> search(left, mTime)
        }
    }

    return search(lowerBound, upperBound)
}

fun main() {
    val scan = Scanner(System.`in`)

    fun readLongList() = scan.nextLine().split(" ").map { it.trim().toInt() }

    val (_, goal) = readLongList()
    val machines = readLongList()

    val ans = minTime(machines, goal)
    println(ans)
}

Compiled from "MinTime.kt"
public final class ru.amai.study.hackerrank.practice.interviewPreparationKit.search.minTime.MinTimeKt {
  public static final long minTime(java.util.List<java.lang.Integer>, int);
    Code:
       0: aload_0
       1: ldc           #10                 // String machines
       3: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: iload_1
       7: i2d
       8: aload_0
       9: invokeinterface #22,  1           // InterfaceMethod java/util/List.size:()I
      14: i2d
      15: ddiv
      16: dstore_2
      17: dload_2
      18: invokestatic  #28                 // Method java/lang/Math.floor:(D)D
      21: d2l
      22: aload_0
      23: checkcast     #30                 // class java/lang/Iterable
      26: invokestatic  #36                 // Method kotlin/collections/CollectionsKt.minOrThrow:(Ljava/lang/Iterable;)Ljava/lang/Comparable;
      29: dup
      30: invokestatic  #40                 // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
      33: checkcast     #42                 // class java/lang/Number
      36: invokevirtual #46                 // Method java/lang/Number.longValue:()J
      39: lmul
      40: lstore        4
      42: dload_2
      43: invokestatic  #49                 // Method java/lang/Math.ceil:(D)D
      46: d2l
      47: aload_0
      48: checkcast     #30                 // class java/lang/Iterable
      51: invokestatic  #52                 // Method kotlin/collections/CollectionsKt.maxOrThrow:(Ljava/lang/Iterable;)Ljava/lang/Comparable;
      54: dup
      55: invokestatic  #40                 // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
      58: checkcast     #42                 // class java/lang/Number
      61: invokevirtual #46                 // Method java/lang/Number.longValue:()J
      64: lmul
      65: lstore        6
      67: iload_1
      68: aload_0
      69: lload         4
      71: lload         6
      73: invokestatic  #56                 // Method minTime$search:(ILjava/util/List;JJ)J
      76: lreturn

  public static final void main();
    Code:
       0: new           #68                 // class java/util/Scanner
       3: dup
       4: getstatic     #74                 // Field java/lang/System.in:Ljava/io/InputStream;
       7: invokespecial #78                 // Method java/util/Scanner."<init>":(Ljava/io/InputStream;)V
      10: astore_0
      11: aload_0
      12: invokestatic  #82                 // Method main$readLongList:(Ljava/util/Scanner;)Ljava/util/List;
      15: iconst_1
      16: invokeinterface #86,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      21: checkcast     #42                 // class java/lang/Number
      24: invokevirtual #89                 // Method java/lang/Number.intValue:()I
      27: istore_1
      28: aload_0
      29: invokestatic  #82                 // Method main$readLongList:(Ljava/util/Scanner;)Ljava/util/List;
      32: astore_2
      33: aload_2
      34: iload_1
      35: invokestatic  #91                 // Method minTime:(Ljava/util/List;I)J
      38: lstore_3
      39: getstatic     #95                 // Field java/lang/System.out:Ljava/io/PrintStream;
      42: lload_3
      43: invokevirtual #101                // Method java/io/PrintStream.println:(J)V
      46: return

  public static void main(java.lang.String[]);
    Code:
       0: invokestatic  #107                // Method main:()V
       3: return

  private static final long minTime$producedByMachinesIn(java.util.List<java.lang.Integer>, long);
    Code:
       0: aload_0
       1: checkcast     #30                 // class java/lang/Iterable
       4: astore_3
       5: lconst_0
       6: lstore        4
       8: iconst_0
       9: istore        6
      11: lload         4
      13: lstore        7
      15: aload_3
      16: invokeinterface #116,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      21: astore        9
      23: aload         9
      25: invokeinterface #122,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
      30: ifeq          72
      33: aload         9
      35: invokeinterface #126,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      40: astore        10
      42: lload         7
      44: aload         10
      46: checkcast     #42                 // class java/lang/Number
      49: invokevirtual #89                 // Method java/lang/Number.intValue:()I
      52: istore        11
      54: lstore        12
      56: iconst_0
      57: istore        14
      59: lload         12
      61: lload_1
      62: iload         11
      64: i2l
      65: ldiv
      66: ladd
      67: lstore        7
      69: goto          23
      72: lload         7
      74: lreturn

  private static final long minTime$search(int, java.util.List<java.lang.Integer>, long, long);
    Code:
       0: lload         4
       2: lload_2
       3: lcmp
       4: ifle          11
       7: iconst_1
       8: goto          12
      11: iconst_0
      12: ifne          67
      15: iconst_0
      16: istore        7
      18: new           #141                // class java/lang/StringBuilder
      21: dup
      22: invokespecial #143                // Method java/lang/StringBuilder."<init>":()V
      25: ldc           #145                // String right (
      27: invokevirtual #149                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      30: lload         4
      32: invokevirtual #152                // Method java/lang/StringBuilder.append:(J)Ljava/lang/StringBuilder;
      35: ldc           #154                // String ) should be > left (
      37: invokevirtual #149                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      40: lload_2
      41: invokevirtual #152                // Method java/lang/StringBuilder.append:(J)Ljava/lang/StringBuilder;
      44: bipush        41
      46: invokevirtual #157                // Method java/lang/StringBuilder.append:(C)Ljava/lang/StringBuilder;
      49: invokevirtual #161                // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      52: astore        7
      54: new           #163                // class java/lang/IllegalStateException
      57: dup
      58: aload         7
      60: invokevirtual #164                // Method java/lang/Object.toString:()Ljava/lang/String;
      63: invokespecial #167                // Method java/lang/IllegalStateException."<init>":(Ljava/lang/String;)V
      66: athrow
      67: lload_2
      68: lload         4
      70: ladd
      71: iconst_2
      72: i2l
      73: ldiv
      74: lstore        6
      76: aload_1
      77: lload         6
      79: invokestatic  #169                // Method minTime$producedByMachinesIn:(Ljava/util/List;J)J
      82: lstore        8
      84: nop
      85: lload         8
      87: iload_0
      88: i2l
      89: lcmp
      90: ifge          107
      93: iload_0
      94: aload_1
      95: lload         6
      97: lconst_1
      98: ladd
      99: lload         4
     101: invokestatic  #56                 // Method minTime$search:(ILjava/util/List;JJ)J
     104: goto          142
     107: lload         8
     109: iload_0
     110: i2l
     111: lcmp
     112: iflt          134
     115: aload_1
     116: lload         6
     118: lconst_1
     119: lsub
     120: invokestatic  #169                // Method minTime$producedByMachinesIn:(Ljava/util/List;J)J
     123: iload_0
     124: i2l
     125: lcmp
     126: ifge          134
     129: lload         6
     131: goto          142
     134: iload_0
     135: aload_1
     136: lload_2
     137: lload         6
     139: invokestatic  #56                 // Method minTime$search:(ILjava/util/List;JJ)J
     142: lreturn

  private static final java.util.List<java.lang.Integer> main$readLongList(java.util.Scanner);
    Code:
       0: aload_0
       1: invokevirtual #179                // Method java/util/Scanner.nextLine:()Ljava/lang/String;
       4: dup
       5: ldc           #181                // String nextLine(...)
       7: invokestatic  #184                // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
      10: checkcast     #186                // class java/lang/CharSequence
      13: iconst_1
      14: anewarray     #188                // class java/lang/String
      17: astore_1
      18: aload_1
      19: iconst_0
      20: ldc           #190                // String
      22: aastore
      23: aload_1
      24: iconst_0
      25: iconst_0
      26: bipush        6
      28: aconst_null
      29: invokestatic  #196                // Method kotlin/text/StringsKt.split$default:(Ljava/lang/CharSequence;[Ljava/lang/String;ZIILjava/lang/Object;)Ljava/util/List;
      32: checkcast     #30                 // class java/lang/Iterable
      35: astore_1
      36: iconst_0
      37: istore_2
      38: aload_1
      39: astore_3
      40: new           #198                // class java/util/ArrayList
      43: dup
      44: aload_1
      45: bipush        10
      47: invokestatic  #202                // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
      50: invokespecial #205                // Method java/util/ArrayList."<init>":(I)V
      53: checkcast     #207                // class java/util/Collection
      56: astore        4
      58: iconst_0
      59: istore        5
      61: aload_3
      62: invokeinterface #116,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      67: astore        6
      69: aload         6
      71: invokeinterface #122,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
      76: ifeq          133
      79: aload         6
      81: invokeinterface #126,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      86: astore        7
      88: aload         4
      90: aload         7
      92: checkcast     #188                // class java/lang/String
      95: astore        8
      97: astore        10
      99: iconst_0
     100: istore        9
     102: nop
     103: aload         8
     105: checkcast     #186                // class java/lang/CharSequence
     108: invokestatic  #211                // Method kotlin/text/StringsKt.trim:(Ljava/lang/CharSequence;)Ljava/lang/CharSequence;
     111: invokevirtual #164                // Method java/lang/Object.toString:()Ljava/lang/String;
     114: invokestatic  #217                // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
     117: nop
     118: invokestatic  #221                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     121: aload         10
     123: swap
     124: invokeinterface #225,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     129: pop
     130: goto          69
     133: aload         4
     135: checkcast     #18                 // class java/util/List
     138: nop
     139: areturn
}