public int min(int start, int end, int total, boolean left) {
    if (start == end) {
      int lcost = w[start] * l;
      int rcost = w[end] * r;
      if (left)
        lcost += Ql;
      else
        rcost += Qr;
      return total + Math.min(lcost, rcost);
    } else {
      if (left) {
        int lcost = total;
        for (int x = start; x <= end; x++)
          lcost += w[x] * l + Ql;
        return Math.min(lcost, min(start, end - 1, total + w[end] * r, false));
      } else {
        int rcost = total;
        for (int x = 0; x <= end; x++)
          rcost += w[x] * r + Qr;
        return Math.min(rcost, min(start + 1, end, total + w[start] * l, true));
      }
    }
  }