title: how to increase the size limit of a mutable list in kotlin?
body: <p>I was attempting to solve the multiset question (<a href="https://codeforces.com/contest/1354/problem/D" rel="nofollow noreferrer">https://codeforces.com/contest/1354/problem/D</a>) on codeforces using Fenwick Tree Data structure. I passed the sample test cases but got the memory limit error after submitting, the testcase is mentioned below.
  (Basically the testcase is:</p>
  <p>1000000 1000000</p>
  <p>1.............1 //10^6 times</p>
  <p>-1...........-1 //10^6 times).</p>
  <p>I tried similar testcase in my IDE and got the below mentioned error.
  (Similar to above, the testcase I provided is:</p>
  <p>1000000 1</p>
  <p>1.............1 //10^6 times</p>
  <p>-1</p>
  <p>)</p>
  <p><em>Exception in thread &quot;main&quot; java.lang.IndexOutOfBoundsException: Index 524289 out of bounds for length 524289
  at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
  at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
  at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
  at java.base/java.util.Objects.checkIndex(Objects.java:373)
  at java.base/java.util.ArrayList.get(ArrayList.java:426)
  at MultisetKt.main(multiset.kt:47)
  at MultisetKt.main(multiset.kt)</em></p>
  <p>Here is my code:</p>
  <pre><code>private fun readInt() = readLine()!!.split(&quot; &quot;).map { it.toInt() }
  
  fun main() {
      var (n, q) = readInt()
      var list = readInt() //modify the list to store it from index 1
      var finalList = listOf(0) + list
      val query = readInt()
      var bit  = MutableList(n+1){0}
  
      fun update(i:Int, value:Int) {
          var index = i
          while(index &lt; n){
              bit.set (index , bit[index] + value)
              index += (index and -index)
          }
      }
      fun rangefunc(i:Int): Int {
          var su = 0
          var index = i
          while(index &gt; 0){
              su += bit[index]
              index -= (index and -index)
          }
          return su
      }
      fun find(x:Int):Int {
          var l = 1
          var r = n
          var ans = n
          var mid = 0
          while (l &lt;= r) {
              mid = (l + r) / 2
              if (rangefunc(mid) &gt;= x) {
                  ans = mid
                  r = mid - 1
              } else {
                  l = mid + 1
              }
          }
          return ans
      }
      for (i in 1..n) {
          update(finalList[i], 1)
      }
      for (j in 0..q - 1) {
          if (query[j] &gt; 0) {
              update(query[j], 1)
          } else {
              update(find(-query[j]), -1)
          }
      }
      if(rangefunc(n) == 0){
          println(0)
      }else{
          println(find(1))
      }
  }
  
  
  </code></pre>
  <p>I believe this is because the BITlist is not able to store 10^6 elements but not sure. Please let me know what changes should I make in my code also any additional advice on how to deal with such cases in the future.</p>
  <p>Thank you in advance :)</p>