Wednesday, April 6, 2016

Chef and Ballons

Chef and Ballons

Today a plane was hijacked by a maniac. All the passengers of the flight are taken as hostage. Chef is also one of them.

He invited one of the passengers to play a game with him. If he loses the game, he will release all the passengers, otherwise he will kill all of them. A high risk affair it is.

Chef volunteered for this tough task. He was blindfolded by Hijacker. Hijacker brought a big black bag from his pockets. The contents of the bag is not visible. He tells Chef that the bag contains R red, G green and B blue colored balloons.

Hijacker now asked Chef to take out some balloons from the box such that there are at least K balloons of the same color and hand him over. If the taken out balloons does not contain at least K balloons of the same color, then the hijacker will shoot everybody. Chef is very scared and wants to leave this game as soon as possible, so he will draw the minimum number of balloons so as to save the passengers. Can you please help scared Chef to find out the minimum number of balloons he should take out.
Input

The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.
The first line of each test case contains a three space-separated integers R, G and B.
The second line contains only one integer K.
Output

For each test case, output a single line containing one integer - the minimum number of balloons Chef need to take out from the bag.
Constraints

    1 ≤ T ≤ 1000
    1 ≤ R, G, B ≤ 109
    1 ≤ K ≤ max{R, G, B}

Subtasks

    Subtask 1 (44 points): 1 ≤ R, G, B ≤ 10
    Subtask 2 (56 points): No additional constraints

Example

Input:
2
3 3 3
1
3 3 3
2

Output:
1
4

Explanation

Example case 2. In the worst-case scenario first three balloons will be of the three different colors and only after fourth balloon Chef will have two balloons of the same color. So, Chef might need to fetch 4 balloons

 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
    #include<iostream>
    using namespace std;
     
    int main(){
     int testcases;
     cin>>testcases;
     testcases=0;
     while(testcases){
      unsigned long red,green,blue,goal,total;
      cin>>red>>green>>blue;
      cin>>goal;
      total=1;
      if(red<goal)
       total+=red;
      else
       total+=goal-1;
      if(green<goal)
       total+=green;
      else
       total+=goal-1;
      if(blue<goal)
       total+=blue;
      else
       total+=goal-1;
      cout<<total<<endl;
      testcases--;
     }
     return 0;
    } 

No comments:

Post a Comment