TopCoder problem "TicketPrinters" used in SRM 481 (Division I Level Three)

Problem Statement

    After 2 years of hard work, it is finally time to unveil the ultimate plan to win the lottery. This plan involves mostly cartomancy, falsification and programming. The tarot specialist has already predicted the N numbers of the required lottery tickets, and they are given as a int[] wantedValues. The falsification specialist has prepared a set of N ticket printers and he has taken a number of security measures to make it very hard to discover the scheme:

  • The printers are in separate locations. For convenience, they are all hidden inside houses that are all on a single street. The distances between each consecutive pair of printers are given by int[] printerDistance, which contains (n-1) elements. For each i, the time required to travel between printer i and printer i+1 is printerDistance[i] seconds.
  • The way a printer functions is by keeping a hidden integer number in its memory. For security, increasing or decreasing the number by one requires 1 second. At any time, it is possible to order the printer to print the number it has in memory. This procedure also takes 1 second. The starting number for printer i is startingValues[i]
  • Each printer can print at most one ticket.
Your role as the programmer is to go to each printer and print the required lottery tickets given by wantedValues. You are currently in the same location as printer number currentPrinter. Since you do not understand why they even hired a programmer for the scheme, you have decided to use programs to save time in this process. The first program you have developed can, given a wanted number, modify the one in the printer by automatically increasing or decreasing it and finally print the wanted number automatically (increasing/decreasing the numbers and printing them still requires one second). You can install this program to each printer once you are its location. The installation doesn't take any time. Once the program is installed, it can work without any administration from your side, so you can immediately proceed to the next printer.

Now your task is to develop the second program that should determine in which order to visit printers and which printer to use for printing of each of the tickets. Return the outcome of the second program, i.e., the minimum time that is necessary to finish printing all the required numbers.


Parameters:int, int[], int[], int[]
Method signature:int minTime(int currentPrinter, int[] printerDistance, int[] startingValues, int[] wantedValues)
(be sure your method is public)


-wantedValues will contain between 1 and 16 elements, inclusive.
-startingValues will contain n elements, where n is the number of elements in wantedValues.
-printerDistance will contain exactly n-1 elements.
-currentPrinter will be between 0 and n-1, inclusive.
-Each element of startingValues, printerDistance and wantedValues will be between 1 and 1000000, inclusive.
-Each element of wantedValues will be unique.


{100, 20, 50}
{66, 78, 99, 5}
{99, 5, 78, 66}
Returns: 171
{50, 50}
{100, 200, 300}
{101, 201, 302}
Returns: 152
A possible way to print the tickets is:

0 seconds: Start printing program up at printer 1 to print ticket #201. Begin moving to printer 2's position.

1 second: Printer 1 reaches number 201 in memory.

2 seconds: Printer 1 finishes printing its assigned ticket.

50 seconds: Arrive at printer 2's location. Order it to print ticket #302. Begin moving to printer 0's position.

52 seconds: Printer 2 reaches number 302 in memory.

53 seconds: Printer 2 finishes printing its assigned ticket.

150 seconds: Arrive at printer 0's location. Order it to print ticket #101.

151 seconds: Printer #0 reaches number #101 in memory.

152 seconds: Printer #0 finishes printing ticket #101.
{13, 26, 39, 13}
{123, 12, 32, 67, 1015}
{1, 2, 3, 4, 5}
Returns: 1063

Problem url:

Problem stats url:




PabloGilberto , ivan_metelsky , Chmel_Tolstiy

Problem categories:

Brute Force, Graph Theory, Search