P22195 [CodeChef] MAXAGRY
普及
题目描述

Alice and Bob were playing a game yet again but this time, Bob already lost the game. Now he wants to take revenge.

Bob saw a jigsaw puzzle which was solved by Alice and decided to jumble the puzzle pieces again. There are N puzzle pieces in a line numbered from 1 to N in increasing order and he has K minutes before Alice catches him.
Bob can swap any two pieces of the puzzle. Each swap takes 1 minute, and so Bob can make at most K swaps.

Let A_{i} denote the piece in the i-th position after Bob finishes making his swaps.

Alice's angriness is defined to be the number of pairs (i, j) such that 1 ≤ i < j ≤ N and A_{i} > A_{j}.

Bob would like to make Alice as angry as possible, so he asks for your help: if he performs his swaps optimally, what is the maximum angriness he can achieve?

------ Input Format ------

  • The first line of input contains an integer T, denoting the number of test cases.
  • The first and only line of each test case contains two space-separated integers N and K: the number of pieces and the number of swaps Bob can make.

------ Output Format ------

  • For each test case, output on a new line a single integer: Alice's maximum possible angriness.

------ Constraints ------

$1 ≤ T ≤ 10^{5}$
$1 ≤ N ≤ 10^{9}$
$1 ≤ K ≤ 10^{9}$

----- Sample Input 1 ------
4
7 3
1 4
8 2
5 3

----- Sample Output 1 ------
21
0
22
10

----- explanation 1 ------
Test case $1$: $N = 7$, so the pieces are initially arranged as $[1, 2, 3, 4, 5, 6, 7]$. Bob can make $K = 3$ swaps. One optimal way of swapping is as follows:
- First, swap $2$ and $6$. Now the pieces are $[1, 6, 3, 4, 5, 2, 7]$
- Next, swap $1$ and $7$. Now the pieces are $[7, 6, 3, 4, 5, 2, 1]$
- Finally, swap $3$ and $5$. Now the pieces are $[7, 6, 5, 4, 3, 2, 1]$

The angriness of this sequence is $21$, which is the maximum possible.

Test case $2$: $N = 1$, so no swaps can be made. The answer is always $0$.

输入格式

见题面

输出格式

见题面

样例输入
4 7 3 1 4 8 2 5 3
样例输出
21 0 22 10