Dial Maximum

0

0 votes
medium, Dynamic programming, Arrays, 2d, Introduction to Dynamic Programming-2, Algorithms, Medium, Dynamic Programming
Problem

Consider a dial pad of 1 to 9 digits as shown in the figure. You can not press a single digit 2 times consecutively. If you are pressing any digit, then after pressing that digit, you can only press its adjacent digit. Two digits are adjacent if they share an edge.
Cost of pressing a digit after another adjacent digit is given. Initially, you have X unit(s) of money. You need to maximize the sum of digit(s) you can press in unit(s) of money. You can start from any digit.

Input :

  • The first line of input contains an integer X, denoting amount of money you have.
  • Each of the following 12 lines contains uv and w, where w is the cost of changing digit from u to v or v to u.

Output :

  • A single integer representing the maximum sum of numbers you can press in X unit of money.

Constraints :

  • 1X,w105
  • 1u,v9

 

 

Sample Input
15
1 2 1
2 3 1
4 5 1
5 6 1
7 8 1
8 9 1
1 4 1
2 5 1
3 6 1
4 7 1
5 8 1
6 9 1
Sample Output
136
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

we can follow given sequence : [9,8,9,8,9,8,9,8,9,8,9,8,9,8,9,8]

Editor Image

?