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 →

  • rajesh969414
    2 years ago+ 0 comments

    // C++ solution for those who stuck in DP

    include

    define ll long long

    define ld long double

    define vec vector

    using namespace std; ll n, k; ll dp[20][1024]; ll fun(ll start, vec> &cost, ll avail) { if(start == n) { return 0; } if(dp[start][avail] != -1) { return dp[start][avail]; } ll ans = -1; for(ll end = start; end < n; end++) { for(ll j = 0; j < k; j++) { if((1ll << j) & avail) { ll tmp = fun(end+1, cost, avail^(1ll<= 0) { if(ans == -1) { ans = cost[j][end] - (start > 0 ? cost[j][start-1] : 0) + tmp; } else { ans = min(ans, cost[j][end] - (start > 0 ? cost[j][start-1] : 0) + tmp); } } } } } return dp[start][avail] = ans; } int main() { cin >> n >> k; vec> a(k, vec(n)); for(ll i = 0; i < k; i++) { cin >> a[i][0]; for(ll j = 1; j < n; j++) { cin >> a[i][j]; a[i][j] += a[i][j-1]; } } memset(dp, -1, sizeof(dp)); cout << fun(0, a, (1ll<

    0|
    Permalink
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy