##### Taum and Bday Hackerrank

Taum is planning to celebrate the birthday of his friend, Diksha. There are two types of gifts that Diksha wants from Taum: one is black and the other is white. To make her happy, Taum has to buy B number of black gifts and W number of white gifts.

The cost of each black gift is X units.

The cost of every white gift is Y units.

The cost of converting each black gift into white gift or vice versa is Z units.

Help Taum by deducing the minimum amount he needs to spend on Diksha’s gifts.

Input Format

The first line will contain an integer T which will be the number of test cases.

There will be pairs of lines. The first line of each test case will contain the values of integers A and B. Another line of each test case will contain the values of integers X, Y, and Z.

Constraints

1<=T<=10
1<=A,B,X,Y,Z<=10^{9}

Output Format

T lines, each containing an integer: the minimum amount of units Taum needs to spend on gifts.

Sample Input

5

10 10

1 1 1

5 9

2 3 4

3 6

9 1 1

7 7

4 2 1

3 3

1 9 2

Sample Output

20

37

12

35

12

Explanation

Sample Case #01:

There is no benefit to converting the white gifts into black or the black gifts into white, so Taum will have to buy each gift for 1 unit. So cost of buying all gifts will be: 10*1+10*1=20.

Sample Case #02:

Again, we can’t decrease the cost of black or white gifts by converting colors. We will buy gifts at their original price. So cost of buying all gifts will be: 5*2+9*3=37.

Sample Case #03:

We will buy white gifts at their original price, 1. For black gifts, we will first buy white one and color them to black, so that their cost will be reduced to 1+1=2. So cost of buying all gifts will be: 3*2+6*1.

Sample Case #04:

Similarly, we will buy white gifts at their original price, 2. For black gifts, we will first buy white one and color them to black, so that their cost will be reduced to 2+1=3. So cost of buying all gifts will be: 7*3+7*2=35.

Sample Case #05: We will buy black gifts at their original price1, . For white gifts, we will first black gifts worth 1 unit and color them to white with another 2 units, so cost for white gifts is reduced to 3 units. So cost of buying all gifts will be: 3*3+3*1=12.

##### Solution

The problem can be easily solved with the fact that there will be only three cases.

Tau buys B black and W white gift.

Tau buys all gifts black.

Tau buys all gift white.

We can take all three cases one by one, and the case that required the least will be the answer in which required cost in minimum. See setter’s code for clear implementation.

The above logic can be proved using the fact that, if is getting minimum at any case other than first one, and he is buying more than B number of black then he will prefer will buy all black. Same thing applies to white as well.

#include<stdio.h> #include<algorithm> #include<iostream> #include<cassert> using namespace std; #define ll long long int #define md 2000000007 #define maxval md-7 int main() { int tests; cin >> tests; while(tests--) { //input ll even, odd, even_price, odd_price, conversion_rate; cin >> even >> odd >> even_price >> odd_price >> conversion_rate; //calculations ll ans = (ll)md*(ll)md; ll as_it_is = even_price * even+odd_price * odd; ans = min(ans, as_it_is); ll price_all_even = odd_price * odd + even * (odd_price + conversion_rate); ll price_all_odd = even_price * even + odd * (even_price + conversion_rate); ans = min(ans, min(price_all_even, price_all_odd)); // printing output cout << ans << endl; } return 0; }

```
for _ in range(input()):
e, o = map(int, raw_input().split())
x, y, z = map(int, raw_input().split())
print min(e * x + o * y, x * (e + o) + o * z, y * (e + o) + e * z)
```

```
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int t=in.nextInt();
for(int k=0;k<t;k++){
BigInteger b=new BigInteger(in.next());
BigInteger w=new BigInteger(in.next());
BigInteger x=new BigInteger(in.next());
BigInteger y=new BigInteger(in.next());
BigInteger z=new BigInteger(in.next());
if(x.compareTo(y.add(z))==1){
System.out.println((b.multiply(y.add(z))).add(w.multiply(y)));
}else if(y.compareTo(x.add(z))==1){
System.out.println(b.multiply(x).add(w.multiply(x.add(z))));
}else System.out.println(b.multiply(x).add(w.multiply(y)));
}
}
}
```