class Node(val name: String, val folders: MutableMap<String, Node>, val files: MutableMap<String, Long>);
val parents: MutableMap<Node, Node> = mutableMapOf<Node, Node>();
val folderSizes: MutableMap<Node, Long> = mutableMapOf();

fun sumFiles(cur: Node, limit: Long): Pair<Long, Long> {
    var size: Long = cur.files.values.sum();
    var ret: Long = 0;
	for(subfolder in cur.folders.keys) {
		val (folderSize, ans) = sumFiles(cur.folders[subfolder]!!, limit);
        size += folderSize;
        ret += ans;
    }
    folderSizes.put(cur, size);
    if (size <= limit) ret += size;
    return Pair(size, ret);
}

fun main() {
    val input = ""
    var root = Node("/", mutableMapOf<String, Node>(), mutableMapOf<String, Long>());
    var currentNode: Node? = null;
    for(s in input.split("\n")) {
        //println(s + " " + currentNode?.name);
        if (!s.startsWith("$")) {
            if (s.startsWith("dir")) {
                val subfolder = s.split(" ")[1];
                val newNode = Node(subfolder, mutableMapOf(), mutableMapOf());
                currentNode!!.folders.put(subfolder, newNode);
                parents.put(newNode, currentNode);
            } else {
                val fileName = s.split(" ")[1];
                val fileSize = Integer.parseInt(s.split(" ")[0]);
                currentNode!!.files.put(fileName, fileSize.toLong());
            }
        } else
		if (s == "$ cd /") currentNode = root; else
        if (s == "$ cd ..") currentNode = parents[currentNode!!]; else
        if (s.startsWith("$ cd")) {
            //println(currentNode!!.folders);
            currentNode = currentNode!!.folders[s.split(" ")[2]];
        } else {
            if (s != "$ ls") println("oops " + s);
        }
    }

    println(sumFiles(root, 100000).second);

    val totalSize = folderSizes[root]!!;
    var ans: Long = 1000000000;
    for(entry in folderSizes.entries.iterator()) {
        val size = entry.value;
        if (70000000 - totalSize + size >= 30000000 && size < ans) {
            ans = size;
        }
    }
    println(ans);
}

Compiled from "day7.kt"
public final class Day7Kt {
  private static final java.util.Map<Node, Node> parents;

  private static final java.util.Map<Node, java.lang.Long> folderSizes;

  public static final java.util.Map<Node, Node> getParents();
    Code:
       0: getstatic     #12                 // Field parents:Ljava/util/Map;
       3: areturn

  public static final java.util.Map<Node, java.lang.Long> getFolderSizes();
    Code:
       0: getstatic     #17                 // Field folderSizes:Ljava/util/Map;
       3: areturn

  public static final kotlin.Pair<java.lang.Long, java.lang.Long> sumFiles(Node, long);
    Code:
       0: aload_0
       1: ldc           #22                 // String cur
       3: invokestatic  #28                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: invokevirtual #33                 // Method Node.getFiles:()Ljava/util/Map;
      10: invokeinterface #39,  1           // InterfaceMethod java/util/Map.values:()Ljava/util/Collection;
      15: checkcast     #41                 // class java/lang/Iterable
      18: invokestatic  #47                 // Method kotlin/collections/CollectionsKt.sumOfLong:(Ljava/lang/Iterable;)J
      21: lstore_3
      22: lconst_0
      23: lstore        5
      25: aload_0
      26: invokevirtual #50                 // Method Node.getFolders:()Ljava/util/Map;
      29: invokeinterface #54,  1           // InterfaceMethod java/util/Map.keySet:()Ljava/util/Set;
      34: invokeinterface #60,  1           // InterfaceMethod java/util/Set.iterator:()Ljava/util/Iterator;
      39: astore        7
      41: aload         7
      43: invokeinterface #66,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      48: ifeq          128
      51: aload         7
      53: invokeinterface #70,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      58: checkcast     #72                 // class java/lang/String
      61: astore        8
      63: aload_0
      64: invokevirtual #50                 // Method Node.getFolders:()Ljava/util/Map;
      67: aload         8
      69: invokeinterface #76,  2           // InterfaceMethod java/util/Map.get:(Ljava/lang/Object;)Ljava/lang/Object;
      74: dup
      75: invokestatic  #80                 // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
      78: checkcast     #30                 // class Node
      81: lload_1
      82: invokestatic  #82                 // Method sumFiles:(LNode;J)Lkotlin/Pair;
      85: astore        9
      87: aload         9
      89: invokevirtual #87                 // Method kotlin/Pair.component1:()Ljava/lang/Object;
      92: checkcast     #89                 // class java/lang/Number
      95: invokevirtual #93                 // Method java/lang/Number.longValue:()J
      98: lstore        10
     100: aload         9
     102: invokevirtual #96                 // Method kotlin/Pair.component2:()Ljava/lang/Object;
     105: checkcast     #89                 // class java/lang/Number
     108: invokevirtual #93                 // Method java/lang/Number.longValue:()J
     111: lstore        12
     113: lload_3
     114: lload         10
     116: ladd
     117: lstore_3
     118: lload         5
     120: lload         12
     122: ladd
     123: lstore        5
     125: goto          41
     128: getstatic     #17                 // Field folderSizes:Ljava/util/Map;
     131: aload_0
     132: lload_3
     133: invokestatic  #102                // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
     136: invokeinterface #106,  3          // InterfaceMethod java/util/Map.put:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
     141: pop
     142: lload_3
     143: lload_1
     144: lcmp
     145: ifgt          154
     148: lload         5
     150: lload_3
     151: ladd
     152: lstore        5
     154: new           #84                 // class kotlin/Pair
     157: dup
     158: lload_3
     159: invokestatic  #102                // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
     162: lload         5
     164: invokestatic  #102                // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
     167: invokespecial #110                // Method kotlin/Pair."<init>":(Ljava/lang/Object;Ljava/lang/Object;)V
     170: areturn

  public static final void main();
    Code:
       0: ldc           #123                // String
       2: astore_0
       3: new           #30                 // class Node
       6: dup
       7: ldc           #125                // String /
       9: new           #127                // class java/util/LinkedHashMap
      12: dup
      13: invokespecial #129                // Method java/util/LinkedHashMap."<init>":()V
      16: checkcast     #35                 // class java/util/Map
      19: new           #127                // class java/util/LinkedHashMap
      22: dup
      23: invokespecial #129                // Method java/util/LinkedHashMap."<init>":()V
      26: checkcast     #35                 // class java/util/Map
      29: invokespecial #132                // Method Node."<init>":(Ljava/lang/String;Ljava/util/Map;Ljava/util/Map;)V
      32: astore_1
      33: aconst_null
      34: astore_2
      35: aload_0
      36: checkcast     #134                // class java/lang/CharSequence
      39: iconst_1
      40: anewarray     #72                 // class java/lang/String
      43: astore        4
      45: aload         4
      47: iconst_0
      48: ldc           #136                // String \n
      50: aastore
      51: aload         4
      53: iconst_0
      54: iconst_0
      55: bipush        6
      57: aconst_null
      58: invokestatic  #142                // Method kotlin/text/StringsKt.split$default:(Ljava/lang/CharSequence;[Ljava/lang/String;ZIILjava/lang/Object;)Ljava/util/List;
      61: invokeinterface #145,  1          // InterfaceMethod java/util/List.iterator:()Ljava/util/Iterator;
      66: astore_3
      67: aload_3
      68: invokeinterface #66,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      73: ifeq          470
      76: aload_3
      77: invokeinterface #70,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      82: checkcast     #72                 // class java/lang/String
      85: astore        4
      87: aload         4
      89: ldc           #147                // String $
      91: iconst_0
      92: iconst_2
      93: aconst_null
      94: invokestatic  #151                // Method kotlin/text/StringsKt.startsWith$default:(Ljava/lang/String;Ljava/lang/String;ZILjava/lang/Object;)Z
      97: ifne          319
     100: aload         4
     102: ldc           #153                // String dir
     104: iconst_0
     105: iconst_2
     106: aconst_null
     107: invokestatic  #151                // Method kotlin/text/StringsKt.startsWith$default:(Ljava/lang/String;Ljava/lang/String;ZILjava/lang/Object;)Z
     110: ifeq          215
     113: aload         4
     115: checkcast     #134                // class java/lang/CharSequence
     118: iconst_1
     119: anewarray     #72                 // class java/lang/String
     122: astore        6
     124: aload         6
     126: iconst_0
     127: ldc           #155                // String
     129: aastore
     130: aload         6
     132: iconst_0
     133: iconst_0
     134: bipush        6
     136: aconst_null
     137: invokestatic  #142                // Method kotlin/text/StringsKt.split$default:(Ljava/lang/CharSequence;[Ljava/lang/String;ZIILjava/lang/Object;)Ljava/util/List;
     140: iconst_1
     141: invokeinterface #158,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     146: checkcast     #72                 // class java/lang/String
     149: astore        5
     151: new           #30                 // class Node
     154: dup
     155: aload         5
     157: new           #127                // class java/util/LinkedHashMap
     160: dup
     161: invokespecial #129                // Method java/util/LinkedHashMap."<init>":()V
     164: checkcast     #35                 // class java/util/Map
     167: new           #127                // class java/util/LinkedHashMap
     170: dup
     171: invokespecial #129                // Method java/util/LinkedHashMap."<init>":()V
     174: checkcast     #35                 // class java/util/Map
     177: invokespecial #132                // Method Node."<init>":(Ljava/lang/String;Ljava/util/Map;Ljava/util/Map;)V
     180: astore        6
     182: aload_2
     183: dup
     184: invokestatic  #80                 // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
     187: invokevirtual #50                 // Method Node.getFolders:()Ljava/util/Map;
     190: aload         5
     192: aload         6
     194: invokeinterface #106,  3          // InterfaceMethod java/util/Map.put:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
     199: pop
     200: getstatic     #12                 // Field parents:Ljava/util/Map;
     203: aload         6
     205: aload_2
     206: invokeinterface #106,  3          // InterfaceMethod java/util/Map.put:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
     211: pop
     212: goto          67
     215: aload         4
     217: checkcast     #134                // class java/lang/CharSequence
     220: iconst_1
     221: anewarray     #72                 // class java/lang/String
     224: astore        6
     226: aload         6
     228: iconst_0
     229: ldc           #155                // String
     231: aastore
     232: aload         6
     234: iconst_0
     235: iconst_0
     236: bipush        6
     238: aconst_null
     239: invokestatic  #142                // Method kotlin/text/StringsKt.split$default:(Ljava/lang/CharSequence;[Ljava/lang/String;ZIILjava/lang/Object;)Ljava/util/List;
     242: iconst_1
     243: invokeinterface #158,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     248: checkcast     #72                 // class java/lang/String
     251: astore        5
     253: aload         4
     255: checkcast     #134                // class java/lang/CharSequence
     258: iconst_1
     259: anewarray     #72                 // class java/lang/String
     262: astore        7
     264: aload         7
     266: iconst_0
     267: ldc           #155                // String
     269: aastore
     270: aload         7
     272: iconst_0
     273: iconst_0
     274: bipush        6
     276: aconst_null
     277: invokestatic  #142                // Method kotlin/text/StringsKt.split$default:(Ljava/lang/CharSequence;[Ljava/lang/String;ZIILjava/lang/Object;)Ljava/util/List;
     280: iconst_0
     281: invokeinterface #158,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     286: checkcast     #72                 // class java/lang/String
     289: invokestatic  #164                // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
     292: istore        6
     294: aload_2
     295: dup
     296: invokestatic  #80                 // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
     299: invokevirtual #33                 // Method Node.getFiles:()Ljava/util/Map;
     302: aload         5
     304: iload         6
     306: i2l
     307: invokestatic  #102                // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
     310: invokeinterface #106,  3          // InterfaceMethod java/util/Map.put:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
     315: pop
     316: goto          67
     319: aload         4
     321: ldc           #166                // String $ cd /
     323: invokestatic  #170                // Method kotlin/jvm/internal/Intrinsics.areEqual:(Ljava/lang/Object;Ljava/lang/Object;)Z
     326: ifeq          334
     329: aload_1
     330: astore_2
     331: goto          67
     334: aload         4
     336: ldc           #172                // String $ cd ..
     338: invokestatic  #170                // Method kotlin/jvm/internal/Intrinsics.areEqual:(Ljava/lang/Object;Ljava/lang/Object;)Z
     341: ifeq          364
     344: getstatic     #12                 // Field parents:Ljava/util/Map;
     347: aload_2
     348: dup
     349: invokestatic  #80                 // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
     352: invokeinterface #76,  2           // InterfaceMethod java/util/Map.get:(Ljava/lang/Object;)Ljava/lang/Object;
     357: checkcast     #30                 // class Node
     360: astore_2
     361: goto          67
     364: aload         4
     366: ldc           #174                // String $ cd
     368: iconst_0
     369: iconst_2
     370: aconst_null
     371: invokestatic  #151                // Method kotlin/text/StringsKt.startsWith$default:(Ljava/lang/String;Ljava/lang/String;ZILjava/lang/Object;)Z
     374: ifeq          430
     377: aload_2
     378: dup
     379: invokestatic  #80                 // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
     382: invokevirtual #50                 // Method Node.getFolders:()Ljava/util/Map;
     385: aload         4
     387: checkcast     #134                // class java/lang/CharSequence
     390: iconst_1
     391: anewarray     #72                 // class java/lang/String
     394: astore        5
     396: aload         5
     398: iconst_0
     399: ldc           #155                // String
     401: aastore
     402: aload         5
     404: iconst_0
     405: iconst_0
     406: bipush        6
     408: aconst_null
     409: invokestatic  #142                // Method kotlin/text/StringsKt.split$default:(Ljava/lang/CharSequence;[Ljava/lang/String;ZIILjava/lang/Object;)Ljava/util/List;
     412: iconst_2
     413: invokeinterface #158,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     418: invokeinterface #76,  2           // InterfaceMethod java/util/Map.get:(Ljava/lang/Object;)Ljava/lang/Object;
     423: checkcast     #30                 // class Node
     426: astore_2
     427: goto          67
     430: aload         4
     432: ldc           #176                // String $ ls
     434: invokestatic  #170                // Method kotlin/jvm/internal/Intrinsics.areEqual:(Ljava/lang/Object;Ljava/lang/Object;)Z
     437: ifne          67
     440: new           #178                // class java/lang/StringBuilder
     443: dup
     444: invokespecial #179                // Method java/lang/StringBuilder."<init>":()V
     447: ldc           #181                // String oops
     449: invokevirtual #185                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     452: aload         4
     454: invokevirtual #185                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     457: invokevirtual #189                // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     460: getstatic     #195                // Field java/lang/System.out:Ljava/io/PrintStream;
     463: swap
     464: invokevirtual #200                // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     467: goto          67
     470: aload_1
     471: ldc2_w        #201                // long 100000l
     474: invokestatic  #82                 // Method sumFiles:(LNode;J)Lkotlin/Pair;
     477: invokevirtual #205                // Method kotlin/Pair.getSecond:()Ljava/lang/Object;
     480: checkcast     #89                 // class java/lang/Number
     483: invokevirtual #93                 // Method java/lang/Number.longValue:()J
     486: lstore_3
     487: getstatic     #195                // Field java/lang/System.out:Ljava/io/PrintStream;
     490: lload_3
     491: invokevirtual #208                // Method java/io/PrintStream.println:(J)V
     494: getstatic     #17                 // Field folderSizes:Ljava/util/Map;
     497: aload_1
     498: invokeinterface #76,  2           // InterfaceMethod java/util/Map.get:(Ljava/lang/Object;)Ljava/lang/Object;
     503: dup
     504: invokestatic  #80                 // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
     507: checkcast     #89                 // class java/lang/Number
     510: invokevirtual #93                 // Method java/lang/Number.longValue:()J
     513: lstore_3
     514: ldc2_w        #209                // long 1000000000l
     517: lstore        5
     519: getstatic     #17                 // Field folderSizes:Ljava/util/Map;
     522: invokeinterface #213,  1          // InterfaceMethod java/util/Map.entrySet:()Ljava/util/Set;
     527: invokeinterface #60,  1           // InterfaceMethod java/util/Set.iterator:()Ljava/util/Iterator;
     532: astore        7
     534: aload         7
     536: invokeinterface #66,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     541: ifeq          601
     544: aload         7
     546: invokeinterface #70,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     551: checkcast     #215                // class java/util/Map$Entry
     554: astore        8
     556: aload         8
     558: invokeinterface #218,  1          // InterfaceMethod java/util/Map$Entry.getValue:()Ljava/lang/Object;
     563: checkcast     #89                 // class java/lang/Number
     566: invokevirtual #93                 // Method java/lang/Number.longValue:()J
     569: lstore        9
     571: ldc           #219                // int 70000000
     573: i2l
     574: lload_3
     575: lsub
     576: lload         9
     578: ladd
     579: ldc2_w        #220                // long 30000000l
     582: lcmp
     583: iflt          534
     586: lload         9
     588: lload         5
     590: lcmp
     591: ifge          534
     594: lload         9
     596: lstore        5
     598: goto          534
     601: getstatic     #195                // Field java/lang/System.out:Ljava/io/PrintStream;
     604: lload         5
     606: invokevirtual #208                // Method java/io/PrintStream.println:(J)V
     609: return

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

  static {};
    Code:
       0: new           #127                // class java/util/LinkedHashMap
       3: dup
       4: invokespecial #129                // Method java/util/LinkedHashMap."<init>":()V
       7: checkcast     #35                 // class java/util/Map
      10: putstatic     #12                 // Field parents:Ljava/util/Map;
      13: new           #127                // class java/util/LinkedHashMap
      16: dup
      17: invokespecial #129                // Method java/util/LinkedHashMap."<init>":()V
      20: checkcast     #35                 // class java/util/Map
      23: putstatic     #17                 // Field folderSizes:Ljava/util/Map;
      26: return
}


fun main() {
    val test = ""
    val di = listOf(-1, 0, 1, 0);
    val dj = listOf(0, 1, 0, -1);
    val mat = test.split("\n");
    val n = mat.size;
    val m = mat[0].length;
    val best = Array(n) {Array(m) {n * m} };
    val next: MutableList<Pair<Int, Int>> = mutableListOf();
    for((i, s) in mat.withIndex()) {
        for(j in 0 until s.length) {
            // remove the 'a' clause for first subproblem
            if (mat[i][j] == 'S' || mat[i][j] == 'a') {
                next.add(Pair(i, j));
                best[i][j] = 0;
            }
        }
    }

    var curInd = 0;
    while(curInd < next.size) {
        val cur = next[curInd++];
        for(d in 0..3) {
            val newPos = Pair(cur.first + di[d], cur.second + dj[d]);
            if (newPos.first < 0 || newPos.first >= n || newPos.second < 0 || newPos.second >= m) continue;
            val curHeight = if (mat[cur.first][cur.second] == 'S') 0 else mat[cur.first][cur.second] - 'a';
            val newHeight = if (mat[newPos.first][newPos.second] == 'E') 25 else mat[newPos.first][newPos.second] - 'a';
            if (curHeight + 1 >= newHeight &&
                best[newPos.first][newPos.second] > best[cur.first][cur.second] + 1) {
                best[newPos.first][newPos.second] = best[cur.first][cur.second] + 1;
                next.add(newPos);
            }
        }
    }
    for((i, s) in mat.withIndex()) {
        for(j in 0 until s.length) {
            if (mat[i][j] == 'E') {
                println(best[i][j]);
            }
        }
    }

}

Compiled from "day12.kt"
public final class Day12Kt {
  public static final void main();
    Code:
       0: ldc           #8                  // String
       2: astore_0
       3: iconst_4
       4: anewarray     #10                 // class java/lang/Integer
       7: astore_2
       8: aload_2
       9: iconst_0
      10: iconst_m1
      11: invokestatic  #14                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      14: aastore
      15: aload_2
      16: iconst_1
      17: iconst_0
      18: invokestatic  #14                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      21: aastore
      22: aload_2
      23: iconst_2
      24: iconst_1
      25: invokestatic  #14                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      28: aastore
      29: aload_2
      30: iconst_3
      31: iconst_0
      32: invokestatic  #14                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      35: aastore
      36: aload_2
      37: invokestatic  #20                 // Method kotlin/collections/CollectionsKt.listOf:([Ljava/lang/Object;)Ljava/util/List;
      40: astore_1
      41: iconst_4
      42: anewarray     #10                 // class java/lang/Integer
      45: astore_3
      46: aload_3
      47: iconst_0
      48: iconst_0
      49: invokestatic  #14                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      52: aastore
      53: aload_3
      54: iconst_1
      55: iconst_1
      56: invokestatic  #14                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      59: aastore
      60: aload_3
      61: iconst_2
      62: iconst_0
      63: invokestatic  #14                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      66: aastore
      67: aload_3
      68: iconst_3
      69: iconst_m1
      70: invokestatic  #14                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      73: aastore
      74: aload_3
      75: invokestatic  #20                 // Method kotlin/collections/CollectionsKt.listOf:([Ljava/lang/Object;)Ljava/util/List;
      78: astore_2
      79: aload_0
      80: checkcast     #22                 // class java/lang/CharSequence
      83: iconst_1
      84: anewarray     #24                 // class java/lang/String
      87: astore        4
      89: aload         4
      91: iconst_0
      92: ldc           #26                 // String \n
      94: aastore
      95: aload         4
      97: iconst_0
      98: iconst_0
      99: bipush        6
     101: aconst_null
     102: invokestatic  #32                 // Method kotlin/text/StringsKt.split$default:(Ljava/lang/CharSequence;[Ljava/lang/String;ZIILjava/lang/Object;)Ljava/util/List;
     105: astore_3
     106: aload_3
     107: invokeinterface #38,  1           // InterfaceMethod java/util/List.size:()I
     112: istore        4
     114: aload_3
     115: iconst_0
     116: invokeinterface #42,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     121: checkcast     #24                 // class java/lang/String
     124: invokevirtual #45                 // Method java/lang/String.length:()I
     127: istore        5
     129: iconst_0
     130: istore        7
     132: iload         4
     134: anewarray     #47                 // class "[Ljava/lang/Integer;"
     137: astore        8
     139: iload         7
     141: iload         4
     143: if_icmpge     211
     146: iload         7
     148: istore        9
     150: aload         8
     152: iload         9
     154: iconst_0
     155: istore        10
     157: iload         5
     159: anewarray     #10                 // class java/lang/Integer
     162: astore        11
     164: istore        17
     166: astore        16
     168: iload         10
     170: iload         5
     172: if_icmpge     198
     175: iload         10
     177: istore        12
     179: aload         11
     181: iload         12
     183: iload         4
     185: iload         5
     187: imul
     188: invokestatic  #14                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     191: aastore
     192: iinc          10, 1
     195: goto          168
     198: aload         16
     200: iload         17
     202: aload         11
     204: aastore
     205: iinc          7, 1
     208: goto          139
     211: aload         8
     213: astore        6
     215: new           #49                 // class java/util/ArrayList
     218: dup
     219: invokespecial #52                 // Method java/util/ArrayList."<init>":()V
     222: checkcast     #34                 // class java/util/List
     225: astore        7
     227: aload_3
     228: checkcast     #54                 // class java/lang/Iterable
     231: invokeinterface #58,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     236: astore        8
     238: iconst_0
     239: istore        9
     241: aload         8
     243: invokeinterface #64,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     248: ifeq          375
     251: iload         9
     253: istore        10
     255: iload         9
     257: iconst_1
     258: iadd
     259: istore        9
     261: aload         8
     263: invokeinterface #68,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     268: checkcast     #24                 // class java/lang/String
     271: astore        11
     273: iconst_0
     274: istore        12
     276: aload         11
     278: invokevirtual #45                 // Method java/lang/String.length:()I
     281: istore        13
     283: iload         12
     285: iload         13
     287: if_icmpge     241
     290: aload_3
     291: iload         10
     293: invokeinterface #42,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     298: checkcast     #24                 // class java/lang/String
     301: iload         12
     303: invokevirtual #72                 // Method java/lang/String.charAt:(I)C
     306: bipush        83
     308: if_icmpeq     332
     311: aload_3
     312: iload         10
     314: invokeinterface #42,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     319: checkcast     #24                 // class java/lang/String
     322: iload         12
     324: invokevirtual #72                 // Method java/lang/String.charAt:(I)C
     327: bipush        97
     329: if_icmpne     369
     332: aload         7
     334: new           #74                 // class kotlin/Pair
     337: dup
     338: iload         10
     340: invokestatic  #14                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     343: iload         12
     345: invokestatic  #14                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     348: invokespecial #77                 // Method kotlin/Pair."<init>":(Ljava/lang/Object;Ljava/lang/Object;)V
     351: invokeinterface #81,  2           // InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z
     356: pop
     357: aload         6
     359: iload         10
     361: aaload
     362: iload         12
     364: iconst_0
     365: invokestatic  #14                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     368: aastore
     369: iinc          12, 1
     372: goto          283
     375: iconst_0
     376: istore        8
     378: iload         8
     380: aload         7
     382: invokeinterface #38,  1           // InterfaceMethod java/util/List.size:()I
     387: if_icmpge     859
     390: aload         7
     392: iload         8
     394: iinc          8, 1
     397: invokeinterface #42,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     402: checkcast     #74                 // class kotlin/Pair
     405: astore        9
     407: iconst_0
     408: istore        10
     410: iload         10
     412: iconst_4
     413: if_icmpge     378
     416: new           #74                 // class kotlin/Pair
     419: dup
     420: aload         9
     422: invokevirtual #84                 // Method kotlin/Pair.getFirst:()Ljava/lang/Object;
     425: checkcast     #86                 // class java/lang/Number
     428: invokevirtual #89                 // Method java/lang/Number.intValue:()I
     431: aload_1
     432: iload         10
     434: invokeinterface #42,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     439: checkcast     #86                 // class java/lang/Number
     442: invokevirtual #89                 // Method java/lang/Number.intValue:()I
     445: iadd
     446: invokestatic  #14                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     449: aload         9
     451: invokevirtual #92                 // Method kotlin/Pair.getSecond:()Ljava/lang/Object;
     454: checkcast     #86                 // class java/lang/Number
     457: invokevirtual #89                 // Method java/lang/Number.intValue:()I
     460: aload_2
     461: iload         10
     463: invokeinterface #42,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     468: checkcast     #86                 // class java/lang/Number
     471: invokevirtual #89                 // Method java/lang/Number.intValue:()I
     474: iadd
     475: invokestatic  #14                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     478: invokespecial #77                 // Method kotlin/Pair."<init>":(Ljava/lang/Object;Ljava/lang/Object;)V
     481: astore        11
     483: aload         11
     485: invokevirtual #84                 // Method kotlin/Pair.getFirst:()Ljava/lang/Object;
     488: checkcast     #86                 // class java/lang/Number
     491: invokevirtual #89                 // Method java/lang/Number.intValue:()I
     494: iflt          853
     497: aload         11
     499: invokevirtual #84                 // Method kotlin/Pair.getFirst:()Ljava/lang/Object;
     502: checkcast     #86                 // class java/lang/Number
     505: invokevirtual #89                 // Method java/lang/Number.intValue:()I
     508: iload         4
     510: if_icmpge     853
     513: aload         11
     515: invokevirtual #92                 // Method kotlin/Pair.getSecond:()Ljava/lang/Object;
     518: checkcast     #86                 // class java/lang/Number
     521: invokevirtual #89                 // Method java/lang/Number.intValue:()I
     524: iflt          853
     527: aload         11
     529: invokevirtual #92                 // Method kotlin/Pair.getSecond:()Ljava/lang/Object;
     532: checkcast     #86                 // class java/lang/Number
     535: invokevirtual #89                 // Method java/lang/Number.intValue:()I
     538: iload         5
     540: if_icmplt     546
     543: goto          853
     546: aload_3
     547: aload         9
     549: invokevirtual #84                 // Method kotlin/Pair.getFirst:()Ljava/lang/Object;
     552: checkcast     #86                 // class java/lang/Number
     555: invokevirtual #89                 // Method java/lang/Number.intValue:()I
     558: invokeinterface #42,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     563: checkcast     #24                 // class java/lang/String
     566: aload         9
     568: invokevirtual #92                 // Method kotlin/Pair.getSecond:()Ljava/lang/Object;
     571: checkcast     #86                 // class java/lang/Number
     574: invokevirtual #89                 // Method java/lang/Number.intValue:()I
     577: invokevirtual #72                 // Method java/lang/String.charAt:(I)C
     580: bipush        83
     582: if_icmpne     589
     585: iconst_0
     586: goto          626
     589: aload_3
     590: aload         9
     592: invokevirtual #84                 // Method kotlin/Pair.getFirst:()Ljava/lang/Object;
     595: checkcast     #86                 // class java/lang/Number
     598: invokevirtual #89                 // Method java/lang/Number.intValue:()I
     601: invokeinterface #42,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     606: checkcast     #24                 // class java/lang/String
     609: aload         9
     611: invokevirtual #92                 // Method kotlin/Pair.getSecond:()Ljava/lang/Object;
     614: checkcast     #86                 // class java/lang/Number
     617: invokevirtual #89                 // Method java/lang/Number.intValue:()I
     620: invokevirtual #72                 // Method java/lang/String.charAt:(I)C
     623: bipush        97
     625: isub
     626: istore        12
     628: aload_3
     629: aload         11
     631: invokevirtual #84                 // Method kotlin/Pair.getFirst:()Ljava/lang/Object;
     634: checkcast     #86                 // class java/lang/Number
     637: invokevirtual #89                 // Method java/lang/Number.intValue:()I
     640: invokeinterface #42,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     645: checkcast     #24                 // class java/lang/String
     648: aload         11
     650: invokevirtual #92                 // Method kotlin/Pair.getSecond:()Ljava/lang/Object;
     653: checkcast     #86                 // class java/lang/Number
     656: invokevirtual #89                 // Method java/lang/Number.intValue:()I
     659: invokevirtual #72                 // Method java/lang/String.charAt:(I)C
     662: bipush        69
     664: if_icmpne     672
     667: bipush        25
     669: goto          709
     672: aload_3
     673: aload         11
     675: invokevirtual #84                 // Method kotlin/Pair.getFirst:()Ljava/lang/Object;
     678: checkcast     #86                 // class java/lang/Number
     681: invokevirtual #89                 // Method java/lang/Number.intValue:()I
     684: invokeinterface #42,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     689: checkcast     #24                 // class java/lang/String
     692: aload         11
     694: invokevirtual #92                 // Method kotlin/Pair.getSecond:()Ljava/lang/Object;
     697: checkcast     #86                 // class java/lang/Number
     700: invokevirtual #89                 // Method java/lang/Number.intValue:()I
     703: invokevirtual #72                 // Method java/lang/String.charAt:(I)C
     706: bipush        97
     708: isub
     709: istore        13
     711: iload         12
     713: iconst_1
     714: iadd
     715: iload         13
     717: if_icmplt     853
     720: aload         6
     722: aload         11
     724: invokevirtual #84                 // Method kotlin/Pair.getFirst:()Ljava/lang/Object;
     727: checkcast     #86                 // class java/lang/Number
     730: invokevirtual #89                 // Method java/lang/Number.intValue:()I
     733: aaload
     734: aload         11
     736: invokevirtual #92                 // Method kotlin/Pair.getSecond:()Ljava/lang/Object;
     739: checkcast     #86                 // class java/lang/Number
     742: invokevirtual #89                 // Method java/lang/Number.intValue:()I
     745: aaload
     746: invokevirtual #93                 // Method java/lang/Integer.intValue:()I
     749: aload         6
     751: aload         9
     753: invokevirtual #84                 // Method kotlin/Pair.getFirst:()Ljava/lang/Object;
     756: checkcast     #86                 // class java/lang/Number
     759: invokevirtual #89                 // Method java/lang/Number.intValue:()I
     762: aaload
     763: aload         9
     765: invokevirtual #92                 // Method kotlin/Pair.getSecond:()Ljava/lang/Object;
     768: checkcast     #86                 // class java/lang/Number
     771: invokevirtual #89                 // Method java/lang/Number.intValue:()I
     774: aaload
     775: invokevirtual #93                 // Method java/lang/Integer.intValue:()I
     778: iconst_1
     779: iadd
     780: if_icmple     853
     783: aload         6
     785: aload         11
     787: invokevirtual #84                 // Method kotlin/Pair.getFirst:()Ljava/lang/Object;
     790: checkcast     #86                 // class java/lang/Number
     793: invokevirtual #89                 // Method java/lang/Number.intValue:()I
     796: aaload
     797: aload         11
     799: invokevirtual #92                 // Method kotlin/Pair.getSecond:()Ljava/lang/Object;
     802: checkcast     #86                 // class java/lang/Number
     805: invokevirtual #89                 // Method java/lang/Number.intValue:()I
     808: aload         6
     810: aload         9
     812: invokevirtual #84                 // Method kotlin/Pair.getFirst:()Ljava/lang/Object;
     815: checkcast     #86                 // class java/lang/Number
     818: invokevirtual #89                 // Method java/lang/Number.intValue:()I
     821: aaload
     822: aload         9
     824: invokevirtual #92                 // Method kotlin/Pair.getSecond:()Ljava/lang/Object;
     827: checkcast     #86                 // class java/lang/Number
     830: invokevirtual #89                 // Method java/lang/Number.intValue:()I
     833: aaload
     834: invokevirtual #93                 // Method java/lang/Integer.intValue:()I
     837: iconst_1
     838: iadd
     839: invokestatic  #14                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     842: aastore
     843: aload         7
     845: aload         11
     847: invokeinterface #81,  2           // InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z
     852: pop
     853: iinc          10, 1
     856: goto          410
     859: aload_3
     860: checkcast     #54                 // class java/lang/Iterable
     863: invokeinterface #58,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     868: astore        9
     870: iconst_0
     871: istore        10
     873: aload         9
     875: invokeinterface #64,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     880: ifeq          970
     883: iload         10
     885: istore        11
     887: iload         10
     889: iconst_1
     890: iadd
     891: istore        10
     893: aload         9
     895: invokeinterface #68,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     900: checkcast     #24                 // class java/lang/String
     903: astore        12
     905: iconst_0
     906: istore        13
     908: aload         12
     910: invokevirtual #45                 // Method java/lang/String.length:()I
     913: istore        14
     915: iload         13
     917: iload         14
     919: if_icmpge     873
     922: aload_3
     923: iload         11
     925: invokeinterface #42,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     930: checkcast     #24                 // class java/lang/String
     933: iload         13
     935: invokevirtual #72                 // Method java/lang/String.charAt:(I)C
     938: bipush        69
     940: if_icmpne     964
     943: aload         6
     945: iload         11
     947: aaload
     948: iload         13
     950: aaload
     951: invokevirtual #93                 // Method java/lang/Integer.intValue:()I
     954: istore        15
     956: getstatic     #99                 // Field java/lang/System.out:Ljava/io/PrintStream;
     959: iload         15
     961: invokevirtual #105                // Method java/io/PrintStream.println:(I)V
     964: iinc          13, 1
     967: goto          915
     970: return

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


fun main()
{
    var vert = 0
    var edge = 0
    var i = 0
    var j = 0
    var k = 0
    var c = 0
    var inf = 999999;
    var cost = Array(10, {IntArray(10)})
    var adj = Array(10, {IntArray(10)})

    println("Enter no of vertices: ")
    vert = readLine() !!.toInt()
    println("Enter no of Edges: ");
    edge = readLine() !!.toInt()
    println("Enter the EDGE cost: ");

    for (k in 1..edge)
    {
        i = readLine() !!.toInt()
        j = readLine() !!.toInt()
        c = readLine() !!.toInt()
        adj[i][j] = c
        cost[i][j] = c
    }

    for (i in 1..vert)
        for (j in 1..vert)
        {
            if (adj[i][j] == 0 && i != j)
                //If its not a edge put infinity
                adj[i][j] = inf
        }

    for (k in 1..vert)
        for (i in 1..vert)
            for (j in 1..vert)
                //Finding the minimum
                //find minimum path from i to j through k
                if ((adj[i][k] + adj[k][j]) > adj[i][j])
                    adj[i][j] = adj[i][j]
                else
                    adj[i][j] = (adj[i][k] + adj[k][j])

    println("The distance matrix of the graph.");
    // Output the resultant matrix
    for (i in 1..vert)
    {
        for (j in 1..vert)
        {
            if (adj[i][j] != inf)
                print("${adj[i][j]} ");
        }
        println(" ");
    }
}

/*Enter no of vertices:
3
Enter no of Edges:
5
Enter the EDGE cost:
1 2 8

2 1 12

1 3 22

3 1 6

2 3 4
The distance matrix of the graph.
0  8  12
10  0  4
6  14  0*/

Compiled from "Jhonson_Algorithm.kt"
public final class Jhonson_AlgorithmKt {
  public static final void main();
    Code:
       0: iconst_0
       1: istore_0
       2: iconst_0
       3: istore_1
       4: iconst_0
       5: istore_2
       6: iconst_0
       7: istore_3
       8: iconst_0
       9: istore        4
      11: iconst_0
      12: istore        5
      14: ldc           #7                  // int 999999
      16: istore        6
      18: iconst_0
      19: istore        8
      21: bipush        10
      23: anewarray     #9                  // class "[I"
      26: astore        9
      28: iload         8
      30: bipush        10
      32: if_icmpge     54
      35: iload         8
      37: istore        10
      39: aload         9
      41: iload         10
      43: bipush        10
      45: newarray       int
      47: aastore
      48: iinc          8, 1
      51: goto          28
      54: aload         9
      56: astore        7
      58: iconst_0
      59: istore        9
      61: bipush        10
      63: anewarray     #9                  // class "[I"
      66: astore        10
      68: iload         9
      70: bipush        10
      72: if_icmpge     94
      75: iload         9
      77: istore        11
      79: aload         10
      81: iload         11
      83: bipush        10
      85: newarray       int
      87: aastore
      88: iinc          9, 1
      91: goto          68
      94: aload         10
      96: astore        8
      98: ldc           #11                 // String Enter no of vertices:
     100: getstatic     #17                 // Field java/lang/System.out:Ljava/io/PrintStream;
     103: swap
     104: invokevirtual #23                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     107: invokestatic  #29                 // Method kotlin/io/ConsoleKt.readLine:()Ljava/lang/String;
     110: dup
     111: invokestatic  #34                 // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
     114: invokestatic  #40                 // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
     117: istore_0
     118: ldc           #42                 // String Enter no of Edges:
     120: getstatic     #17                 // Field java/lang/System.out:Ljava/io/PrintStream;
     123: swap
     124: invokevirtual #23                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     127: invokestatic  #29                 // Method kotlin/io/ConsoleKt.readLine:()Ljava/lang/String;
     130: dup
     131: invokestatic  #34                 // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
     134: invokestatic  #40                 // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
     137: istore_1
     138: ldc           #44                 // String Enter the EDGE cost:
     140: getstatic     #17                 // Field java/lang/System.out:Ljava/io/PrintStream;
     143: swap
     144: invokevirtual #23                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     147: iconst_1
     148: istore        9
     150: iload_1
     151: istore        10
     153: iload         9
     155: iload         10
     157: if_icmpgt     223
     160: invokestatic  #29                 // Method kotlin/io/ConsoleKt.readLine:()Ljava/lang/String;
     163: dup
     164: invokestatic  #34                 // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
     167: invokestatic  #40                 // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
     170: istore_2
     171: invokestatic  #29                 // Method kotlin/io/ConsoleKt.readLine:()Ljava/lang/String;
     174: dup
     175: invokestatic  #34                 // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
     178: invokestatic  #40                 // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
     181: istore_3
     182: invokestatic  #29                 // Method kotlin/io/ConsoleKt.readLine:()Ljava/lang/String;
     185: dup
     186: invokestatic  #34                 // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
     189: invokestatic  #40                 // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
     192: istore        5
     194: aload         8
     196: iload_2
     197: aaload
     198: iload_3
     199: iload         5
     201: iastore
     202: aload         7
     204: iload_2
     205: aaload
     206: iload_3
     207: iload         5
     209: iastore
     210: iload         9
     212: iload         10
     214: if_icmpeq     223
     217: iinc          9, 1
     220: goto          160
     223: iconst_1
     224: istore        9
     226: iload_0
     227: istore        10
     229: iload         9
     231: iload         10
     233: if_icmpgt     303
     236: iconst_1
     237: istore        11
     239: iload_0
     240: istore        12
     242: iload         11
     244: iload         12
     246: if_icmpgt     290
     249: aload         8
     251: iload         9
     253: aaload
     254: iload         11
     256: iaload
     257: ifne          277
     260: iload         9
     262: iload         11
     264: if_icmpeq     277
     267: aload         8
     269: iload         9
     271: aaload
     272: iload         11
     274: iload         6
     276: iastore
     277: iload         11
     279: iload         12
     281: if_icmpeq     290
     284: iinc          11, 1
     287: goto          249
     290: iload         9
     292: iload         10
     294: if_icmpeq     303
     297: iinc          9, 1
     300: goto          236
     303: iconst_1
     304: istore        9
     306: iload_0
     307: istore        10
     309: iload         9
     311: iload         10
     313: if_icmpgt     453
     316: iconst_1
     317: istore        11
     319: iload_0
     320: istore        12
     322: iload         11
     324: iload         12
     326: if_icmpgt     440
     329: iconst_1
     330: istore        13
     332: iload_0
     333: istore        14
     335: iload         13
     337: iload         14
     339: if_icmpgt     427
     342: aload         8
     344: iload         11
     346: aaload
     347: iload         9
     349: iaload
     350: aload         8
     352: iload         9
     354: aaload
     355: iload         13
     357: iaload
     358: iadd
     359: aload         8
     361: iload         11
     363: aaload
     364: iload         13
     366: iaload
     367: if_icmple     389
     370: aload         8
     372: iload         11
     374: aaload
     375: iload         13
     377: aload         8
     379: iload         11
     381: aaload
     382: iload         13
     384: iaload
     385: iastore
     386: goto          414
     389: aload         8
     391: iload         11
     393: aaload
     394: iload         13
     396: aload         8
     398: iload         11
     400: aaload
     401: iload         9
     403: iaload
     404: aload         8
     406: iload         9
     408: aaload
     409: iload         13
     411: iaload
     412: iadd
     413: iastore
     414: iload         13
     416: iload         14
     418: if_icmpeq     427
     421: iinc          13, 1
     424: goto          342
     427: iload         11
     429: iload         12
     431: if_icmpeq     440
     434: iinc          11, 1
     437: goto          329
     440: iload         9
     442: iload         10
     444: if_icmpeq     453
     447: iinc          9, 1
     450: goto          316
     453: ldc           #46                 // String The distance matrix of the graph.
     455: getstatic     #17                 // Field java/lang/System.out:Ljava/io/PrintStream;
     458: swap
     459: invokevirtual #23                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     462: iconst_1
     463: istore        9
     465: iload_0
     466: istore        10
     468: iload         9
     470: iload         10
     472: if_icmpgt     569
     475: iconst_1
     476: istore        11
     478: iload_0
     479: istore        12
     481: iload         11
     483: iload         12
     485: if_icmpgt     547
     488: aload         8
     490: iload         9
     492: aaload
     493: iload         11
     495: iaload
     496: iload         6
     498: if_icmpeq     534
     501: new           #48                 // class java/lang/StringBuilder
     504: dup
     505: invokespecial #51                 // Method java/lang/StringBuilder."<init>":()V
     508: aload         8
     510: iload         9
     512: aaload
     513: iload         11
     515: iaload
     516: invokevirtual #55                 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
     519: bipush        32
     521: invokevirtual #58                 // Method java/lang/StringBuilder.append:(C)Ljava/lang/StringBuilder;
     524: invokevirtual #61                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     527: getstatic     #17                 // Field java/lang/System.out:Ljava/io/PrintStream;
     530: swap
     531: invokevirtual #64                 // Method java/io/PrintStream.print:(Ljava/lang/Object;)V
     534: iload         11
     536: iload         12
     538: if_icmpeq     547
     541: iinc          11, 1
     544: goto          488
     547: ldc           #66                 // String
     549: getstatic     #17                 // Field java/lang/System.out:Ljava/io/PrintStream;
     552: swap
     553: invokevirtual #23                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     556: iload         9
     558: iload         10
     560: if_icmpeq     569
     563: iinc          9, 1
     566: goto          475
     569: return

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


/*
<NAME> String Searching Algorithm
Given a text txt[0..n-1] and a pattern pat[0..m-1], the algo will find all occurrences of pat[] in txt[]
*/

import java.util.*

internal class KMP {
    fun kmpSearch(pat: String, txt: String) {
        val m = pat.length
        val n = txt.length
        // longest_prefix_suffix array
        val lps = IntArray(m)

        // index for pat[]
        var j = 0

        //calculate lps[] array
        computeLPSArray(pat, m, lps)

        //index for txt[]
        var i = 0
        while (i < n) {
            if (pat[j] == txt[i]) {
                j++
                i++
            }
            if (j == m) {
                println("Found pattern at index" + (i - j))
                j = lps[j - 1]
            } else if (i < n && pat[j] != txt[i]) {
                if (j != 0) j = lps[j - 1] else i += 1
            }
        }
    }

    // length of the previous longest prefix suffix
    private fun computeLPSArray(pat: String, M: Int, lps: IntArray) {
        var len = 0
        var i = 1

        //lps[0] is always 0
        lps[0] = 0

        //calculate lps[i] for i=1 to M-1
        while (i < M)
        {
            if (pat[i] == pat[len]) {
                len++
                lps[i] = len
                i++
            }
            else {
                if (len != 0) len = lps[len - 1] else {
                    lps[i] = len
                    i++
                }
            }
        }
    }

    companion object {
        //Driver program to test above function
        @JvmStatic
        fun main(args: Array<String>) {
            val sc = Scanner(System.`in`)
            val txt = sc.nextLine()
            val pat = sc.nextLine()
            KMP().kmpSearch(pat, txt)
        }
    }
}

/*
Sample Input
namanchamanbomanamansanam
aman

Sample Output:
Patterns occur at shift = 1
Patterns occur at shift = 7
Patterns occur at shift = 16
 */

Compiled from "KMP.kt"
public final class KMP {
  public static final KMP$Companion Companion;

  public KMP();
    Code:
       0: aload_0
       1: invokespecial #8                  // Method java/lang/Object."<init>":()V
       4: return

  public final void kmpSearch(java.lang.String, java.lang.String);
    Code:
       0: aload_1
       1: ldc           #15                 // String pat
       3: invokestatic  #21                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_2
       7: ldc           #23                 // String txt
       9: invokestatic  #21                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      12: aload_1
      13: invokevirtual #29                 // Method java/lang/String.length:()I
      16: istore_3
      17: aload_2
      18: invokevirtual #29                 // Method java/lang/String.length:()I
      21: istore        4
      23: iload_3
      24: newarray       int
      26: astore        5
      28: iconst_0
      29: istore        6
      31: aload_0
      32: aload_1
      33: iload_3
      34: aload         5
      36: invokespecial #33                 // Method computeLPSArray:(Ljava/lang/String;I[I)V
      39: iconst_0
      40: istore        7
      42: iload         7
      44: iload         4
      46: if_icmpge     163
      49: aload_1
      50: iload         6
      52: invokevirtual #37                 // Method java/lang/String.charAt:(I)C
      55: aload_2
      56: iload         7
      58: invokevirtual #37                 // Method java/lang/String.charAt:(I)C
      61: if_icmpne     70
      64: iinc          6, 1
      67: iinc          7, 1
      70: iload         6
      72: iload_3
      73: if_icmpne     118
      76: new           #39                 // class java/lang/StringBuilder
      79: dup
      80: invokespecial #40                 // Method java/lang/StringBuilder."<init>":()V
      83: ldc           #42                 // String Found pattern at index
      85: invokevirtual #46                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      88: iload         7
      90: iload         6
      92: isub
      93: invokevirtual #49                 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
      96: invokevirtual #53                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      99: getstatic     #59                 // Field java/lang/System.out:Ljava/io/PrintStream;
     102: swap
     103: invokevirtual #65                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     106: aload         5
     108: iload         6
     110: iconst_1
     111: isub
     112: iaload
     113: istore        6
     115: goto          42
     118: iload         7
     120: iload         4
     122: if_icmpge     42
     125: aload_1
     126: iload         6
     128: invokevirtual #37                 // Method java/lang/String.charAt:(I)C
     131: aload_2
     132: iload         7
     134: invokevirtual #37                 // Method java/lang/String.charAt:(I)C
     137: if_icmpeq     42
     140: iload         6
     142: ifeq          157
     145: aload         5
     147: iload         6
     149: iconst_1
     150: isub
     151: iaload
     152: istore        6
     154: goto          42
     157: iinc          7, 1
     160: goto          42
     163: return

  private final void computeLPSArray(java.lang.String, int, int[]);
    Code:
       0: iconst_0
       1: istore        4
       3: iconst_1
       4: istore        5
       6: aload_3
       7: iconst_0
       8: iconst_0
       9: iastore
      10: iload         5
      12: iload_2
      13: if_icmpge     80
      16: aload_1
      17: iload         5
      19: invokevirtual #37                 // Method java/lang/String.charAt:(I)C
      22: aload_1
      23: iload         4
      25: invokevirtual #37                 // Method java/lang/String.charAt:(I)C
      28: if_icmpne     49
      31: iinc          4, 1
      34: aload_3
      35: iload         5
      37: iload         4
      39: iastore
      40: iload         5
      42: iinc          5, 1
      45: pop
      46: goto          10
      49: iload         4
      51: ifeq          65
      54: aload_3
      55: iload         4
      57: iconst_1
      58: isub
      59: iaload
      60: istore        4
      62: goto          10
      65: aload_3
      66: iload         5
      68: iload         4
      70: iastore
      71: iload         5
      73: iinc          5, 1
      76: pop
      77: goto          10
      80: return

  public static final void main(java.lang.String[]);
    Code:
       0: getstatic     #83                 // Field Companion:LKMP$Companion;
       3: aload_0
       4: invokevirtual #87                 // Method KMP$Companion.main:([Ljava/lang/String;)V
       7: return

  static {};
    Code:
       0: new           #85                 // class KMP$Companion
       3: dup
       4: aconst_null
       5: invokespecial #93                 // Method KMP$Companion."<init>":(Lkotlin/jvm/internal/DefaultConstructorMarker;)V
       8: putstatic     #83                 // Field Companion:LKMP$Companion;
      11: return
}

Compiled from "KMP.kt"
public final class KMP$Companion {
  private KMP$Companion();
    Code:
       0: aload_0
       1: invokespecial #8                  // Method java/lang/Object."<init>":()V
       4: return

  public final void main(java.lang.String[]);
    Code:
       0: aload_1
       1: ldc           #16                 // String args
       3: invokestatic  #22                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: new           #24                 // class java/util/Scanner
       9: dup
      10: getstatic     #30                 // Field java/lang/System.in:Ljava/io/InputStream;
      13: invokespecial #33                 // Method java/util/Scanner."<init>":(Ljava/io/InputStream;)V
      16: astore_2
      17: aload_2
      18: invokevirtual #37                 // Method java/util/Scanner.nextLine:()Ljava/lang/String;
      21: astore_3
      22: aload_2
      23: invokevirtual #37                 // Method java/util/Scanner.nextLine:()Ljava/lang/String;
      26: astore        4
      28: new           #39                 // class KMP
      31: dup
      32: invokespecial #40                 // Method KMP."<init>":()V
      35: aload         4
      37: invokestatic  #44                 // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
      40: aload         4
      42: aload_3
      43: invokestatic  #44                 // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
      46: aload_3
      47: invokevirtual #48                 // Method KMP.kmpSearch:(Ljava/lang/String;Ljava/lang/String;)V
      50: return

  public KMP$Companion(kotlin.jvm.internal.DefaultConstructorMarker);
    Code:
       0: aload_0
       1: invokespecial #56                 // Method "<init>":()V
       4: return
}


/* QuickSort is a Divide and Conquer algorithm. It picks an element as pivot and partitions the given array
around the picked pivot.It's Best case complexity is n*log(n) & Worst case complexity is n^2. */

//partition array
fun quick_sort(A: Array<Int>, p: Int, r: Int) {
    if (p < r) {
        var q: Int = partition(A, p, r)
        quick_sort(A, p, q - 1)
        quick_sort(A, q + 1, r)

    }
}

//assign last value as pivot
fun partition(A: Array<Int>, p: Int, r: Int): Int {
    var x = A[r]
    var i = p - 1
    for (j in p until r) {
        if (A[j] <= x) {
            i++
            exchange(A, i, j)
        }
    }
    exchange(A, i + 1, r)
    return i + 1
}

//swap
fun exchange(A: Array<Int>, i: Int, j: Int) {
    var temp = A[i]
    A[i] = A[j]
    A[j] = temp
}

fun main(arg: Array<String>) {
    print("Enter no. of elements :")
    var n = readLine()!!.toInt()

    println("Enter elements : ")
    var A = Array(n, { 0 })
    for (i in 0 until n)
        A[i] = readLine()!!.toInt()

    quick_sort(A, 0, A.size - 1)

    println("Sorted array is : ")
    for (i in 0 until n)
        print("${A[i]}  ")
}

/*-------OUTPUT--------------
Enter no. of elements :6
Enter elements :
4
8
5
9
2
6
Sorted array is :
2  4  5  6  8  9
*/

Compiled from "Quick_Sort.kt"
public final class Quick_SortKt {
  public static final void quick_sort(java.lang.Integer[], 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: iload_1
       7: iload_2
       8: if_icmpge     34
      11: aload_0
      12: iload_1
      13: iload_2
      14: invokestatic  #19                 // Method partition:([Ljava/lang/Integer;II)I
      17: istore_3
      18: aload_0
      19: iload_1
      20: iload_3
      21: iconst_1
      22: isub
      23: invokestatic  #21                 // Method quick_sort:([Ljava/lang/Integer;II)V
      26: aload_0
      27: iload_3
      28: iconst_1
      29: iadd
      30: iload_2
      31: invokestatic  #21                 // Method quick_sort:([Ljava/lang/Integer;II)V
      34: return

  public static final int partition(java.lang.Integer[], 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_0
       7: iload_2
       8: aaload
       9: invokevirtual #32                 // Method java/lang/Integer.intValue:()I
      12: istore_3
      13: iload_1
      14: iconst_1
      15: isub
      16: istore        4
      18: iload_1
      19: istore        5
      21: iload         5
      23: iload_2
      24: if_icmpge     55
      27: aload_0
      28: iload         5
      30: aaload
      31: invokevirtual #32                 // Method java/lang/Integer.intValue:()I
      34: iload_3
      35: if_icmpgt     49
      38: iinc          4, 1
      41: aload_0
      42: iload         4
      44: iload         5
      46: invokestatic  #35                 // Method exchange:([Ljava/lang/Integer;II)V
      49: iinc          5, 1
      52: goto          21
      55: aload_0
      56: iload         4
      58: iconst_1
      59: iadd
      60: iload_2
      61: invokestatic  #35                 // Method exchange:([Ljava/lang/Integer;II)V
      64: iload         4
      66: iconst_1
      67: iadd
      68: ireturn

  public static final void exchange(java.lang.Integer[], 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_0
       7: iload_1
       8: aaload
       9: invokevirtual #32                 // Method java/lang/Integer.intValue:()I
      12: istore_3
      13: aload_0
      14: iload_1
      15: aload_0
      16: iload_2
      17: aaload
      18: aastore
      19: aload_0
      20: iload_2
      21: iload_3
      22: invokestatic  #42                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      25: aastore
      26: return

  public static final void main(java.lang.String[]);
    Code:
       0: aload_0
       1: ldc           #47                 // String arg
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: ldc           #49                 // String Enter no. of elements :
       8: getstatic     #55                 // Field java/lang/System.out:Ljava/io/PrintStream;
      11: swap
      12: invokevirtual #61                 // Method java/io/PrintStream.print:(Ljava/lang/Object;)V
      15: invokestatic  #67                 // Method kotlin/io/ConsoleKt.readLine:()Ljava/lang/String;
      18: dup
      19: invokestatic  #70                 // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
      22: invokestatic  #74                 // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
      25: istore_1
      26: ldc           #76                 // String Enter elements :
      28: getstatic     #55                 // Field java/lang/System.out:Ljava/io/PrintStream;
      31: swap
      32: invokevirtual #79                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      35: iconst_0
      36: istore_3
      37: iload_1
      38: istore        4
      40: iload         4
      42: anewarray     #28                 // class java/lang/Integer
      45: astore        5
      47: iload_3
      48: iload         4
      50: if_icmpge     71
      53: iload_3
      54: istore        6
      56: aload         5
      58: iload         6
      60: iconst_0
      61: invokestatic  #42                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      64: aastore
      65: iinc          3, 1
      68: goto          47
      71: aload         5
      73: astore_2
      74: iconst_0
      75: istore_3
      76: iload_1
      77: istore        4
      79: iload_3
      80: iload         4
      82: if_icmpge     107
      85: aload_2
      86: iload_3
      87: invokestatic  #67                 // Method kotlin/io/ConsoleKt.readLine:()Ljava/lang/String;
      90: dup
      91: invokestatic  #70                 // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
      94: invokestatic  #74                 // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
      97: invokestatic  #42                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     100: aastore
     101: iinc          3, 1
     104: goto          79
     107: aload_2
     108: iconst_0
     109: aload_2
     110: arraylength
     111: iconst_1
     112: isub
     113: invokestatic  #21                 // Method quick_sort:([Ljava/lang/Integer;II)V
     116: ldc           #81                 // String Sorted array is :
     118: getstatic     #55                 // Field java/lang/System.out:Ljava/io/PrintStream;
     121: swap
     122: invokevirtual #79                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     125: iconst_0
     126: istore_3
     127: iload_1
     128: istore        4
     130: iload_3
     131: iload         4
     133: if_icmpge     173
     136: new           #83                 // class java/lang/StringBuilder
     139: dup
     140: invokespecial #87                 // Method java/lang/StringBuilder."<init>":()V
     143: aload_2
     144: iload_3
     145: aaload
     146: invokevirtual #32                 // Method java/lang/Integer.intValue:()I
     149: invokevirtual #91                 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
     152: ldc           #93                 // String
     154: invokevirtual #96                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     157: invokevirtual #99                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     160: getstatic     #55                 // Field java/lang/System.out:Ljava/io/PrintStream;
     163: swap
     164: invokevirtual #61                 // Method java/io/PrintStream.print:(Ljava/lang/Object;)V
     167: iinc          3, 1
     170: goto          130
     173: return
}


// version 1.1

object FloydWarshall {
    fun doCalcs(weights: Array<IntArray>, nVertices: Int) {
        val dist = Array(nVertices) { DoubleArray(nVertices) { Double.POSITIVE_INFINITY } }
        for (w in weights) dist[w[0] - 1][w[1] - 1] = w[2].toDouble()
        val next = Array(nVertices) { IntArray(nVertices) }
        for (i in 0 until next.size) {
            for (j in 0 until next.size) {
                if (i != j) next[i][j] = j + 1
            }
        }
        for (k in 0 until nVertices) {
            for (i in 0 until nVertices) {
                for (j in 0 until nVertices) {
                    if (dist[i][k] + dist[k][j] < dist[i][j]) {
                        dist[i][j] = dist[i][k] + dist[k][j]
                        next[i][j] = next[i][k]
                    }
                }
            }
        }
        printResult(dist, next)
    }

    private fun printResult(dist: Array<DoubleArray>, next: Array<IntArray>) {
        var u: Int
        var v: Int
        var path: String
        println("pair     dist    path")
        for (i in 0 until next.size) {
            for (j in 0 until next.size) {
                if (i != j) {
                    u = i + 1
                    v = j + 1
                    path = ("%d -> %d    %2d     %s").format(u, v, dist[i][j].toInt(), u)
                    do {
                        u = next[u - 1][v - 1]
                        path += " -> " + u
                    } while (u != v)
                    println(path)
                }
            }
        }
    }
}

fun main(args: Array<String>) {
    val weights = arrayOf(
            intArrayOf(1, 3, -2),
            intArrayOf(2, 1, 4),
            intArrayOf(2, 3, 3),
            intArrayOf(3, 4, 2),
            intArrayOf(4, 2, -1)
    )
    val nVertices = 4
    FloydWarshall.doCalcs(weights, nVertices)
}

/*
*         Output:
*
* pair     dist    path
* 1 -> 2    -1     1 -> 3 -> 4 -> 2
* 1 -> 3    -2     1 -> 3
* 1 -> 4     0     1 -> 3 -> 4
* 2 -> 1     4     2 -> 1
* 2 -> 3     2     2 -> 1 -> 3
* 2 -> 4     4     2 -> 1 -> 3 -> 4
* 3 -> 1     5     3 -> 4 -> 2 -> 1
* 3 -> 2     1     3 -> 4 -> 2
* 3 -> 4     2     3 -> 4
* 4 -> 1     3     4 -> 2 -> 1
* 4 -> 2    -1     4 -> 2
* 4 -> 3     1     4 -> 2 -> 1 -> 3
*
*/

Compiled from "Floyd_Warshall_Algorithm.kt"
public final class Floyd_Warshall_AlgorithmKt {
  public static final void main(java.lang.String[]);
    Code:
       0: aload_0
       1: ldc           #9                  // String args
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: iconst_5
       7: anewarray     #17                 // class "[I"
      10: astore_2
      11: aload_2
      12: iconst_0
      13: iconst_3
      14: newarray       int
      16: astore_3
      17: aload_3
      18: iconst_0
      19: iconst_1
      20: iastore
      21: aload_3
      22: iconst_1
      23: iconst_3
      24: iastore
      25: aload_3
      26: iconst_2
      27: bipush        -2
      29: iastore
      30: aload_3
      31: aastore
      32: aload_2
      33: iconst_1
      34: iconst_3
      35: newarray       int
      37: astore_3
      38: aload_3
      39: iconst_0
      40: iconst_2
      41: iastore
      42: aload_3
      43: iconst_1
      44: iconst_1
      45: iastore
      46: aload_3
      47: iconst_2
      48: iconst_4
      49: iastore
      50: aload_3
      51: aastore
      52: aload_2
      53: iconst_2
      54: iconst_3
      55: newarray       int
      57: astore_3
      58: aload_3
      59: iconst_0
      60: iconst_2
      61: iastore
      62: aload_3
      63: iconst_1
      64: iconst_3
      65: iastore
      66: aload_3
      67: iconst_2
      68: iconst_3
      69: iastore
      70: aload_3
      71: aastore
      72: aload_2
      73: iconst_3
      74: iconst_3
      75: newarray       int
      77: astore_3
      78: aload_3
      79: iconst_0
      80: iconst_3
      81: iastore
      82: aload_3
      83: iconst_1
      84: iconst_4
      85: iastore
      86: aload_3
      87: iconst_2
      88: iconst_2
      89: iastore
      90: aload_3
      91: aastore
      92: aload_2
      93: iconst_4
      94: iconst_3
      95: newarray       int
      97: astore_3
      98: aload_3
      99: iconst_0
     100: iconst_4
     101: iastore
     102: aload_3
     103: iconst_1
     104: iconst_2
     105: iastore
     106: aload_3
     107: iconst_2
     108: iconst_m1
     109: iastore
     110: aload_3
     111: aastore
     112: aload_2
     113: astore_1
     114: iconst_4
     115: istore_2
     116: getstatic     #23                 // Field FloydWarshall.INSTANCE:LFloydWarshall;
     119: aload_1
     120: iload_2
     121: invokevirtual #27                 // Method FloydWarshall.doCalcs:([[II)V
     124: return
}


/*Fenwick Tree is used when there is a problem of range sum query with update
i.e. RMQ. Suppose you have an array and you have been asked to find sum in
range. It can be done in linear time with static array method. If will be
difficult for one to do it in linear time when you have point updates. In this
update operation is incrementing an index by some value. Brute force approach
will take O(n^2) time but Fenwick Tree will take O(nlogn) time
*/

//Sum function
fun sum(ft:Array<Int>, _index:Int):Int{
    /*
    Argument
    ft     : Fenwick Tree
    index  : Index upto which you want to find prefix sum
    Initialize the result "s" then increment the value of
    index "index".

    Returns : sum of given arr[0..index]. This function assumes
    that the array is preprocessed and partial sums of
    array elements are stored in ft[].
    */

    // Initialize sum variable to 0
    var s:Int  = 0
    // Increment index
    var index = _index + 1

    while (index > 0){

        // Adding tree node to sum
        s += ft[index]
        // Update tree node
        index -= index and (-index)
    }
    // Return total sum
    return s
}

// Update function
fun update(ft:Array<Int>,size:Int,_index:Int,value:Int){
    /*
    Arguments
    ft    : Fenwick Tree
    index : Index of ft to be updated
    size  : Length of the original array
    val   : Add val to the index "index"
    Traverse all ancestors and add 'val'.
    Add 'val' to current node of Fenwick Tree.
    Update index to that of parent in update.
    */

    var index = _index + 1
    while (index <= size) {

        // Update tree node value
        ft[index] += value
        // Update node index
        index += index and (-index)
    }

}

// Construct function
fun construct(array:Array<Int>, size:Int):Array<Int>{
    /*
    Argument
    arr : The original array
    size : The length of the given array
    This function will construct the Fenwick Tree
    from the given array of length "size"

    Return : Fenwick Tree array ft[]
    */

    // Init ft array of length size+1 with zero value initially
    var ft = Array(size+1,{0})

    // Constructing Fenwick Tree by Update operation
    for (i in 0 until size){
        // Update operation
        update(ft,size,i,array[i])
    }
    // Return Fenwick Tree array
    return ft
}

fun main(){
    /*
        INPUT
        -------
        arr : [2, 1, 1, 3, 2, 3, 4, 5, 6, 7, 8, 9]
        Print sum of freq[0...5]
        Update position 4 by adding 16
        Print sum of freq[2....7] after update
        Update position 5 by adding 10
        Print sum of freq[2....7] after update

        OUTPUT
        ------
        12
        33
        43
    */

    // Given array
    var list = arrayOf(2, 1, 1, 3, 2, 3, 4, 5, 6, 7, 8, 9)

    // Build Fenwick Tree
    var ft = construct(list,list.size)

    // Print Sum of array from index = 0 to index = 5
    println(sum(ft,5))

    // Increment list[4] by 16
    update(ft,list.size,4,16)

    // Print sum from index = 2 to index = 7
    println(sum(ft,7) - sum(ft,2))

    // Increment list[5] by 10
    update(ft,list.size,5,10)

    // Print sum from index = 2 to index = 7
    println(sum(ft,7) - sum(ft,2))
}

Compiled from "Fenwick_Tree.kt"
public final class Fenwick_TreeKt {
  public static final int sum(java.lang.Integer[], int);
    Code:
       0: aload_0
       1: ldc           #9                  // String ft
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: iconst_0
       7: istore_2
       8: iload_1
       9: iconst_1
      10: iadd
      11: istore_3
      12: iload_3
      13: ifle          35
      16: iload_2
      17: aload_0
      18: iload_3
      19: aaload
      20: invokevirtual #21                 // Method java/lang/Integer.intValue:()I
      23: iadd
      24: istore_2
      25: iload_3
      26: iload_3
      27: iload_3
      28: ineg
      29: iand
      30: isub
      31: istore_3
      32: goto          12
      35: iload_2
      36: ireturn

  public static final void update(java.lang.Integer[], int, int, int);
    Code:
       0: aload_0
       1: ldc           #9                  // String ft
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: iload_2
       7: iconst_1
       8: iadd
       9: istore        4
      11: iload         4
      13: iload_1
      14: if_icmpgt     51
      17: iload         4
      19: istore        5
      21: aload_0
      22: iload         5
      24: aload_0
      25: iload         5
      27: aaload
      28: invokevirtual #21                 // Method java/lang/Integer.intValue:()I
      31: iload_3
      32: iadd
      33: invokestatic  #32                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      36: aastore
      37: iload         4
      39: iload         4
      41: iload         4
      43: ineg
      44: iand
      45: iadd
      46: istore        4
      48: goto          11
      51: return

  public static final java.lang.Integer[] construct(java.lang.Integer[], int);
    Code:
       0: aload_0
       1: ldc           #38                 // String array
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: iconst_0
       7: istore_3
       8: iload_1
       9: iconst_1
      10: iadd
      11: istore        4
      13: iload         4
      15: anewarray     #17                 // class java/lang/Integer
      18: astore        5
      20: iload_3
      21: iload         4
      23: if_icmpge     44
      26: iload_3
      27: istore        6
      29: aload         5
      31: iload         6
      33: iconst_0
      34: invokestatic  #32                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      37: aastore
      38: iinc          3, 1
      41: goto          20
      44: aload         5
      46: astore_2
      47: iconst_0
      48: istore_3
      49: iload_3
      50: iload_1
      51: if_icmpge     72
      54: aload_2
      55: iload_1
      56: iload_3
      57: aload_0
      58: iload_3
      59: aaload
      60: invokevirtual #21                 // Method java/lang/Integer.intValue:()I
      63: invokestatic  #40                 // Method update:([Ljava/lang/Integer;III)V
      66: iinc          3, 1
      69: goto          49
      72: aload_2
      73: areturn

  public static final void main();
    Code:
       0: bipush        12
       2: anewarray     #17                 // class java/lang/Integer
       5: astore_1
       6: aload_1
       7: iconst_0
       8: iconst_2
       9: invokestatic  #32                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      12: aastore
      13: aload_1
      14: iconst_1
      15: iconst_1
      16: invokestatic  #32                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      19: aastore
      20: aload_1
      21: iconst_2
      22: iconst_1
      23: invokestatic  #32                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      26: aastore
      27: aload_1
      28: iconst_3
      29: iconst_3
      30: invokestatic  #32                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      33: aastore
      34: aload_1
      35: iconst_4
      36: iconst_2
      37: invokestatic  #32                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      40: aastore
      41: aload_1
      42: iconst_5
      43: iconst_3
      44: invokestatic  #32                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      47: aastore
      48: aload_1
      49: bipush        6
      51: iconst_4
      52: invokestatic  #32                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      55: aastore
      56: aload_1
      57: bipush        7
      59: iconst_5
      60: invokestatic  #32                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      63: aastore
      64: aload_1
      65: bipush        8
      67: bipush        6
      69: invokestatic  #32                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      72: aastore
      73: aload_1
      74: bipush        9
      76: bipush        7
      78: invokestatic  #32                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      81: aastore
      82: aload_1
      83: bipush        10
      85: bipush        8
      87: invokestatic  #32                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      90: aastore
      91: aload_1
      92: bipush        11
      94: bipush        9
      96: invokestatic  #32                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      99: aastore
     100: aload_1
     101: astore_0
     102: aload_0
     103: aload_0
     104: arraylength
     105: invokestatic  #46                 // Method construct:([Ljava/lang/Integer;I)[Ljava/lang/Integer;
     108: astore_1
     109: aload_1
     110: iconst_5
     111: invokestatic  #48                 // Method sum:([Ljava/lang/Integer;I)I
     114: istore_2
     115: getstatic     #54                 // Field java/lang/System.out:Ljava/io/PrintStream;
     118: iload_2
     119: invokevirtual #60                 // Method java/io/PrintStream.println:(I)V
     122: aload_1
     123: aload_0
     124: arraylength
     125: iconst_4
     126: bipush        16
     128: invokestatic  #40                 // Method update:([Ljava/lang/Integer;III)V
     131: aload_1
     132: bipush        7
     134: invokestatic  #48                 // Method sum:([Ljava/lang/Integer;I)I
     137: aload_1
     138: iconst_2
     139: invokestatic  #48                 // Method sum:([Ljava/lang/Integer;I)I
     142: isub
     143: istore_2
     144: getstatic     #54                 // Field java/lang/System.out:Ljava/io/PrintStream;
     147: iload_2
     148: invokevirtual #60                 // Method java/io/PrintStream.println:(I)V
     151: aload_1
     152: aload_0
     153: arraylength
     154: iconst_5
     155: bipush        10
     157: invokestatic  #40                 // Method update:([Ljava/lang/Integer;III)V
     160: aload_1
     161: bipush        7
     163: invokestatic  #48                 // Method sum:([Ljava/lang/Integer;I)I
     166: aload_1
     167: iconst_2
     168: invokestatic  #48                 // Method sum:([Ljava/lang/Integer;I)I
     171: isub
     172: istore_2
     173: getstatic     #54                 // Field java/lang/System.out:Ljava/io/PrintStream;
     176: iload_2
     177: invokevirtual #60                 // Method java/io/PrintStream.println:(I)V
     180: return

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