HackerRank 'Taum and B'day' Solution

by Srikant Padala on June 9, 2016, 12:41 am


Explanation


  • convert black toy to white toy if x + z < y. We save (y-(x+z))*w) cost by converting.
  • convert white toy to black toy if y + z < x.

Taum and B'day Problem Statement

Video

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <unordered_map>

using namespace std;


int main(){
    int t;
    cin >> t;
    for(int a0 = 0; a0 < t; a0++){
        long b;
        long w;
        cin >> b >> w;
        long x;
        long y;
        long z;
        cin >> x >> y >> z;
        //convert black toy to white toy if x + z < y
        if(x+z < y) {
            cout << b*x + w*(x+z) << endl;
        }
        //convert white toy to black toy if y + z < x
        else if(y+z < x) {
            cout << w*y + b*(y+z) << endl;
        }
        else cout << b*x + w*y << endl;
    }
    return 0;
}

Coming Soon.