Hackerrank – Problem Statement
A description of the problem can be found on Hackerrank.
Solution
The hint is given in problem description. One should compare if it is better to buy presents without changing colors or with changing colors.
I created solution in:
All solutions are also available on my GitHub profile.
Scala
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 |
object TaumAndBday { def main(args: Array[String]) { val sc = new java.util.Scanner (System.in) val t = sc.nextInt() var a0 = 0 while(a0 < t){ val black = sc.nextLong() val white = sc.nextLong() val x = sc.nextLong() val y = sc.nextLong() val z = sc.nextLong() val ansWithoutConvert = black * x + white * y val blackConvertCost = black * z val ansWhite = (white + black) * y + blackConvertCost val whiteConvertCost = white * z val ansBlack = (black + white) * x + whiteConvertCost var min = ansWithoutConvert if(ansBlack.compareTo(min) < 0) { min = ansBlack } if(ansWhite.compareTo(min) < 0) { min = ansWhite } println(min) a0 +=1 } } } |
Java
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 |
import java.math.BigInteger; import java.util.Scanner; public class TaumAndBday { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int tests = Integer.parseInt(scanner.nextLine()); for (int i = 0; i < tests; i++) { String[] counts = scanner.nextLine().split("\\s+"); BigInteger black = new BigInteger(counts[0]); BigInteger white = new BigInteger(counts[1]); String[] costs = scanner.nextLine().split("\\s+"); BigInteger blackCost = new BigInteger(costs[0]); BigInteger whiteCost = new BigInteger(costs[1]); BigInteger convertCost = new BigInteger(costs[2]); BigInteger ansWithoutConvert = black.multiply(blackCost).add( white.multiply(whiteCost)); BigInteger blackConvertCost = black.multiply(convertCost); BigInteger ansWhite = white.add(black).multiply(whiteCost) .add(blackConvertCost); BigInteger whiteConvertCost = white.multiply(convertCost); BigInteger ansBlack = black.add(white).multiply(blackCost) .add(whiteConvertCost); BigInteger min = ansWithoutConvert; if(ansBlack.compareTo(min) < 0) { min = ansBlack; } if(ansWhite.compareTo(min) < 0) { min = ansWhite; } System.out.println(min); } scanner.close(); } } |
Javascript
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 |
'use strict'; process.stdin.resume(); process.stdin.setEncoding('ascii'); var input_stdin = ""; var input_stdin_array = ""; var input_currentline = 0; process.stdin.on('data', function (data) { input_stdin += data; }); process.stdin.on('end', function () { input_stdin_array = input_stdin.split("\n"); main(); }); function readLine() { return input_stdin_array[input_currentline++]; } function main() { const t = parseInt(readLine()); for(let a0 = 0; a0 < t; a0++){ const b_temp = readLine().split(' '); const black = parseInt(b_temp[0]); const white = parseInt(b_temp[1]); const x_temp = readLine().split(' '); const x = parseInt(x_temp[0]); const y = parseInt(x_temp[1]); const z = parseInt(x_temp[2]); const ansWithoutConvert = black * x + white * y; const blackConvertCost = black * z; const ansWhite = (white + black) * y + blackConvertCost; const whiteConvertCost = white * z; const ansBlack = (black + white) * x + whiteConvertCost; let min = ansWithoutConvert; if(ansBlack < min) { min = ansBlack; } if(ansWhite < min) { min = ansWhite; } console.log(min); } } |