Digital string

2.4

5 votes
Algorithms, Approved, Dynamic Programming, Grammar-Verified, Hard, Hiring, Recruit
Problem

A digital string is defined as a string of length N consisting of the digits 2, 4, 6, or 8.

You are given an empty string and the digits have to be entered in this string such that no two adjacent digits are the same.
For each position in the string, you are given four integers P,Q,R,S. These integers denote the cost of entering 2, 4, 6, or 8 respectively in that position in the string.

Write a program to determine the minimum cost of a digital string.
Note: P, Q, R, and S denote the cost of entering the digit 2, 4, 6, or 8 in the string respectively

Input format

  • First line: T denoting the number of test cases
  • For each test case:
    • First line: N
    • Next N lines: ith line of these N lines contain four integers P,Q,R,S denoting the cost of entering 2, 4, 6, or 8 respectively in the ith position in the string

Output format

Print the minimum cost of a digital string.

Constraints

1T10

1N105

P,Q,R,S1000

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

Best choice is to fill 1st space with digit "2" with cost 2 and 2nd space with digit "8" with cost 1.so total cost will be 3 and final digital string will be "28".

Editor Image

?