-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path1353A - Most Unstable Array.cpp
61 lines (44 loc) · 1.9 KB
/
1353A - Most Unstable Array.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
You are given two integers n and m. You have to construct the array a of length n consisting of non-negative integers (i.e. integers greater than or equal to zero) such that the sum of elements of this array is exactly m and the value ?i=1n-1|ai-ai+1| is the maximum possible. Recall that |x| is the absolute value of x.
In other words, you have to maximize the sum of absolute differences between adjacent (consecutive) elements. For example, if the array a=[1,3,2,5,5,0] then the value above for this array is |1-3|+|3-2|+|2-5|+|5-5|+|5-0|=2+1+3+0+5=11. Note that this example doesn't show the optimal answer but it shows how the required value for some array is calculated.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1=t=104) — the number of test cases. Then t test cases follow.
The only line of the test case contains two integers n and m (1=n,m=109) — the length of the array and its sum correspondingly.
Output
For each test case, print the answer — the maximum possible value of ?i=1n-1|ai-ai+1| for the array a consisting of n non-negative integers with the sum m.
Example
inputCopy
5
1 100
2 2
5 5
2 1000000000
1000000000 1000000000
outputCopy
0
2
10
1000000000
2000000000
Note
In the first test case of the example, the only possible array is [100] and the answer is obviously 0.
In the second test case of the example, one of the possible arrays is [2,0] and the answer is |2-0|=2.
In the third test case of the example, one of the possible arrays is [0,2,0,3,0] and the answer is |0-2|+|2-0|+|0-3|+|3-0|=10.
*/
#include<bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
int n, m;
cin >> n >> m;
if (n == 1) cout << 0 << endl;
else if (n == 2) cout << m << endl;
else cout << 2 * m << endl;
}
return 0;
}