Not actually The Travelling Salesman problem
A travelling salesman is visiting different cities selling his wares and making a living. These cities are located on a grid. This grid increases positively along the x-axis towards the right and positively along the y-axis downwards. At each city, the salesman makes a certain amount of coins in sales. Can you calculate his total earnings after the salesman travels a certain distance?
Input Specification
The first line of input contains 2 space separated integers, representing the salesman's starting coordinates.
The next line of input contains representing the number of cities on the grid.
The next lines contain 3 space separated integers, first representing the and coordinates of each city followed by representing the money the salesman would earn should he visit this city.
The next line contains representing the total number of squares the salesman travels.
The next lines each represent a direction in which the salesman traveled. The salesman traveled square in each of these directions in order of when they appear. U
represents upwards travel, D
represents downward travel, L
represents leftward travel, and R
represents rightward travel. It is possible for the salesman to visit the same city more than once, in this case, he will earn the same amount of coins again. It is guaranteed that the salesman's current and position will always remain .
Output Specification
Print how much money the salesman would earn if he were to adhere to this given route.
Sample Input
1 1
3
4 2 7
3 3 12
10 8 6
6
D
D
R
R
U
R
Sample Output
19
Explanation for Sample
The salesman starts at position . There are 3 cities located at with 7, 12 and 6 coins to be earned at each respectively. The salesman travels 6 squares as follows:
D
- The salesman moves to .
D
- The salesman moves to .
R
- The salesman moves to .
R
- The salesman moves to and collects 12 coins from the city there.
U
- The salesman moves to .
R
- The salesman moves to and collects 7 coins from the city there.
The salesman never visits the city at and collects a total of 19 coins.
Comments