Problem:
Score : 200 points

Problem Statement

You have N apples, called Apple 1, Apple 2, Apple 3, ..., Apple N. The flavor of Apple i is L+i-1, which can be negative.

You can make an apple pie using one or more of the apples. The flavor of the apple pie will be the sum of the flavors of the apples used.

You planned to make an apple pie using all of the apples, but being hungry tempts you to eat one of them, which can no longer be used to make the apple pie.

You want to make an apple pie that is as similar as possible to the one that you planned to make. Thus, you will choose the apple to eat so that the flavor of the apple pie made of the remaining N-1 apples will have the smallest possible absolute difference from the flavor of the apple pie made of all the N apples.

Find the flavor of the apple pie made of the remaining N-1 apples when you choose the apple to eat as above.

We can prove that this value is uniquely determined.

Constraints

2 \leq N \leq 200

-100 \leq L \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N L

Output

Find the flavor of the apple pie made of the remaining N-1 apples when you optimally choose the apple to eat.

Sample Input 1

5 2

Sample Output 1

18

The flavors of Apple 1, 2, 3, 4, and 5 are 2, 3, 4, 5, and 6, respectively. The optimal choice is to eat Apple 1, so the answer is 3+4+5+6=18.

Sample Input 2

3 -1

Sample Output 2

0

The flavors of Apple 1, 2, and 3 are -1, 0, and 1, respectively. The optimal choice is to eat Apple 2, so the answer is (-1)+1=0.

Sample Input 3

30 -50

Sample Output 3

-1044

Solution in Scala (2.11.7) (judge verdict: Accepted)

object Main extends App {
  val input = io.StdIn.readLine().split(" ").map(_.toInt)
  val N = input(0)
  val L = input(1)

  val apples = (1 to N).toArray
  val tastes = apples.map { a => L + a - 1 }
  val totalTaste = tastes.sum
  // 全てが正の時→最小の整数
  // 正と負の時→最小の整数
  // 全てが負の時→最大の整数

  val result = if (tastes.head >= 0) tastes.tail.sum
  else if (tastes.last <= 0) tastes.init.sum
  else {
    var isFound = false
    tastes.foldLeft(scala.collection.mutable.ArrayBuffer.empty[Int]) { (acc, v) =>
      if (isFound) {
        acc += v
      } else {
        if (v >= 0) {
          isFound = true
          acc
        } else {
          acc += v
        }
      }
    }.sum
  }

  println(result)
}