public void solve() throws IOException {
    int n = nextInt();
    int m = nextInt();
    
    int mintime = 0;
    int maxtime = 0;
    
    int[] boxes = new int[n];
    boolean zero = true;
    for (int x = 0; x < n; x++) {
      maxtime++;
      
      boxes[x] = nextInt();
      maxtime += Math.ceil((double)boxes[x] / m);
      zero &= (boxes[x] == 0);
    }
    
    if (zero) {
      System.out.println(0);
      return;
    }
    
    while (maxtime > mintime + 1) {
      int middle = (maxtime + mintime) / 2;
      int students = m;
      boolean possible = true;
      int[] add = new int[n];
      for (int x = 0; x < n; x++) {
        int time = x + 1;
        if (boxes[x] > 0) {
          int remaining = middle - time;
          int minimum = (int) Math.ceil((double)boxes[x] / students);
          if (remaining < minimum) {
            possible = false;
          } else {
            int left = (int) Math.ceil((double)boxes[x] / remaining);
            students -= left;
          }
        }
      }
      if (possible) {
        maxtime = middle;
      } else {
        mintime = middle;
      }
    }
    System.out.println(maxtime);
  }