We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Dynamic Programming
  4. The Blacklist
  5. Discussions

The Blacklist

Problem
Submissions
Leaderboard
Discussions
Editorial

    You are viewing a single comment's thread. Return to all comments →

  • shehanjayalath
    2 years ago+ 0 comments

    Python 2 Solution

    from __future__ import print_function
    
    import os
    import sys
    
    N, K = map(int, raw_input().split())
    cost = []
    
    MASK = 2<<K
    INF = 10**8-1
    
    for i in xrange(K):
        prices = map(int, raw_input().split())
        prices.reverse()
        cost.append(prices)
    
    dp = []
    for i in xrange(MASK):
        dp.append([INF] * (N + 1))
    
    dp[0][0] = 0
    
    def hitman_available(hitman, mask):
        return not (2 << hitman) & mask
    
    def use_hitman(hitman, mask):
        return mask | (2 << hitman)
    
    for already_killed in xrange(0, N):
        for mask in xrange(0, MASK):
            for hitman in xrange(0, K):
                if hitman_available(hitman, mask):
                    mask_after = use_hitman(hitman, mask)
                    for num_to_kill in xrange(1, N - already_killed+1):
                        dp[mask_after][num_to_kill + already_killed] = min(
                            dp[mask_after][num_to_kill + already_killed],
                            dp[mask][already_killed] + sum(cost[hitman][already_killed:already_killed+num_to_kill]))
    
    print (min(dp[i][N] for i in xrange(MASK)))
    
    0|
    Permalink
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy