TopcoderArchive
Archive
Algorithm problem

AmountToPay

Problem statement, definition, constraints, and public examples.

Problem Statement

You must write a program which will process bills at the store. You will be given two String[]s, goods and bill, containing information about goods and a bill. Each element of goods will be of the form "ID COST AMOUNT", where ID, COST and AMOUNT are positive integers without leading zeroes. ID is a unique code for the goods, AMOUNT specifies the number of units in the package, and COST specifies the cost of this package. Each element of bill will be formatted as "ID AMOUNT", where ID is the unique code of the goods sold, and AMOUNT is the number of units sold. Your program should compute the total amount for the bill.
Note that, while the goods are priced in packages, customers need not buy full packages. Hence, if a package of 4 units costs 8, then 2 units costs 4, and 6 units costs 12.

Definition

Class:
AmountToPay
Method:
calculate
Parameters:
String[], String[]
Returns:
int
Method signature:
int calculate(String[] goods, String[] bill)
(be sure your method is public)

Notes

  • bill may contain several elements with the same ID.

Constraints

  • goods and bill will each contain between 1 and 50 elements, inclusive.
  • Each element of goods will contain 3 space-delimited integers without any leading zeroes.
  • Each element of bill will contain 2 space-delimited integers without any leading zeroes.
  • Each ID in bill will appear as an ID in goods.
  • For each element of goods, the price for 1 unit of goods will be integral (COST will be always divisible by AMOUNT).
  • All numbers in all elements of goods and bill will be between 1 and 10000, inclusive.
  • All IDs in goods will be unique.

Examples

  1. {"1 1 1", "2 1 1", "3 1 1"}
    {"1 1", "2 1", "3 1", "2 1"}
    Returns: 4
    goods contains information about 3 different goods. bill contains the first and the third of them once, and the second good appears twice. 1 unit of any good costs 1, so 4 units of different goods cost 4.
  2. {"1 1 1", "2 2 2", "3 3 3"}
    {"1 1", "2 1", "3 1", "2 1"}
    Returns: 4
  3. {"123 30 1", "321 20 1", "175 50 2"}
    {"175 1"}
    Returns: 25
  4. {"8292 5868 652", "5073 5100 170", "5566 9092 9092", "662 9107 9107", "1559 7738 3869",
     "6220 8322 8322", "5456 8152 8152", "2257 2007 2007", "8430 8298 8298", "85 9067 9067",
     "8713 7962 7962", "5656 1958 1958", "4872 5947 5947"}
    {"2257 8263", "662 7339", "4872 8165", "4872 9156", "85 6065",
     "5656 6487", "2257 3993", "1559 3022", "2257 950", "1559 5216",
     "5073 7781", "5566 5253", "5566 9106"}
    Returns: 314683
Back to all problems