Problem : https://www.codechef.com/ZCOPRAC/problems/ZCO14002

It’s pure Dynamic classic problem! Greedy solution would not be able to cover all path! You have to use either Dynamic programming or use Priority queue for implementation! See my Solution

No a greedy solution does not exist. This problem is dynamic programming.

My answer is as follows

n = int(input()) c = [int(x) for x in input().split()] if len(c) <= 3: print(min(c)) else: cost = [c[0],c[1],c[2]] for i in range(3,len(c)): cost.append(c[i] + min(cost[i-3:i])) print(min(cost[len(c)-3:]))

Here this pass all test cases there is a problem in test case, there the give value of n but the element they have given in next line, they are greater or lesser then value of n

this was my code btw xD

2 Likes

This is still Dynamic Programming.