**The Blacklist**

### Problem Statement :

A new gangster is trying to take control of the city. He makes a list of his N adversaries (e.g. gangster 1, gangster 2, ... gangster N-1, gangster N) and plans to get rid of them. K mercenaries are willing to do the job. The gangster can use any number of these mercenaries. But he has to honor one condition set by them: they have to be assigned in such a way that they eliminate a consecutive group of gangsters in the list, e.g. gangster i, gangster i+1, ..., gangster j-1, gangster j, where the following is true: 1 <= i <= j <= N. While our new gangster wants to kill all of them, he also wants to pay the least amount of money. All mercenaries charge a different amount to kill different people. So he asks you to help him minimize his expenses. Input Format The first line contains two space-separated integers, N and K. Then K lines follow, each containing N integers as follows: The jth number on the ith line is the amount charged by the ith mercenary for killing the jth gangster on the list. Constraints 1 <= N <= 20 1 <= K <= 10 0 <= amount charged <= 10000 Output Format Just one line, the minimum cost for killing the N gangsters on the list.

### Solution :

` ````
Solution in C :
In C++ :
#include <cstdio>
#include <cmath>
#include <iostream>
#include <set>
#include <algorithm>
#include <vector>
#include <map>
#include <cassert>
#include <string>
#include <cstring>
using namespace std;
#define rep(i,a,b) for(int i = a; i < b; i++)
#define S(x) scanf("%d",&x)
#define P(x) printf("%d\n",x)
typedef long long int LL;
const int INF = 100000000;
int C[10][20];
int memo[1<<10][11][21];
int n,k;
int solve(int mask, int last, int idx) {
// printf("%d %d %d\n",mask,last,idx);
if(last == -1) {
int res = INF;
rep(i,0,k) res = min(res, C[i][idx] + solve(mask|(1<<i), i, idx+1));
return res;
}
if(idx == n) return 0;
int &res = memo[mask][last][idx];
if(memo[mask][last][idx] != INF)
return memo[mask][last][idx];
res = C[last][idx] + min(res, solve(mask, last, idx+1));
rep(i,0,k) if(!((mask>>i)&1)) {
res = min(res, C[i][idx] + solve(mask|(1<<i) , i, idx+1) );
}
return res;
}
int main() {
scanf("%d%d",&n,&k);
rep(i,0,1<<k) rep(j,0,k) rep(l,0,n) memo[i][j][l] = INF;
rep(i,0,k) rep(j,0,n) S(C[i][j]);
printf("%d\n",solve(0, -1, 0));
return 0;
}
In Java :
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int N = input.nextInt();
int K = input.nextInt();
int[][] price = new int[K][N];
for (int k=0; k<K; k++) {
for (int n=0; n<N; n++) {
price[k][n] = input.nextInt();
}
}
int limit = 1<<K;
int[][] dp = new int[N+1][limit+1];
for (int n=0; n<=N; n++) {
for (int x=0; x<=limit; x++) {
dp[n][x] = 1000000;
}
}
dp[0][0] = 0;
for (int n=1; n<=N; n++) {
for (int k=0; k<K; k++) {
int mask = 1 << k;
for (int from=1; from<=n; from++) {
for (int x=0; x<limit; x++) {
if ((x&mask) == 0) {
int newMask = x|mask;
int newValue = dp[from-1][x];
for (int i=from; i<=n; i++) {
newValue += price[k][i-1];
}
if (dp[n][newMask] > newValue) {
dp[n][newMask] = newValue;
}
}
}
}
}
}
int min = Integer.MAX_VALUE;
for (int value : dp[N]) {
min = Math.min(min, value);
}
System.out.println(min);
}
}
In C :
#include<stdio.h>
#include<limits.h>
#define MIN(x,y) x<y?x:y
int res,n,k,a[15][25]={0};
void find(int curr,int d[],int i,int sumsofar)
{
//printf("here %d %d %d %d\n",curr,i,sumsofar,res);
if(sumsofar>=res)
return;
if(curr>n)
{
res=sumsofar;
return;
}
int p[25],x=0;
//find(curr+1,d,i,sumsofar+a[i][curr]);
int j,l;
for(j=1;j<=k;j++)
if(d[j]==0)
p[x++]=j;
for(l=curr;l<=n;l++)
{
for(j=0;j<x;j++)
if(sumsofar+a[p[j]][l]<sumsofar+a[i][l] && sumsofar+a[p[j]][l]<res)
{
// printf("here also %d %d\n",p[j],i);
d[p[j]]=1;
find(l+1,d,p[j],sumsofar+a[p[j]][l]);
d[p[j]]=0;
}
sumsofar+=a[i][l];
if(sumsofar>=res)
return;
}
res=sumsofar;
}
int main()
{
int i,j;
scanf("%d%d",&n,&k);
for(i=1;i<=k;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
/*for(i=1;i<=k;i++)
{for(j=1;j<=n;j++)
{
b[i][j]=b[i][j-1]+a[i][j];
//if(i!=1)
for(j1=1;j1<=i;j1++)
b[i][j]=MIN(b[i][j],(b[j1][j-1]+a[i][j]));
// printf("%d ",b[i][j]);
}
// printf("\n");
}*/
//int min=INT_MAX;
/*for(i=1;i<=k;i++)
if(b[i][n]<min)
min=b[i][n];*/
res=INT_MAX;
int d[25]={0};
for(i=1;i<=k;i++)
{
d[i]=1;
find(1,d,i,0);
d[i]=0;
}
printf("%d\n",res);
return 0;
}
In Python3 :
G, M = [int(s) for s in input().split()]
costs = list()
cache = dict()
for i in range(M):
costs.append([int(s) for s in input().split()])
def kill(gn, imercs):
key = str(gn)+str(imercs)
if key in cache:
return cache[key]
mercs = imercs[:]
if gn == 0:
return 0
temp = list()
for m in mercs:
tempm = mercs[:]
tempm.remove(m)
if len(tempm) == 0:
temp.append(sum(costs[m][:gn]))
else:
for c in range(gn - 1, -1, -1):
temp.append(sum(costs[m][c:gn]) + kill(c, tempm))
cache[key] = min(temp)
return min(temp)
print(str(kill(G, [i for i in range(M)])))
```

## View More Similar Problems

## Find the Running Median

The median of a set of integers is the midpoint value of the data set for which an equal number of integers are less than and greater than the value. To find the median, you must first sort your set of integers in non-decreasing order, then: If your set contains an odd number of elements, the median is the middle element of the sorted sample. In the sorted set { 1, 2, 3 } , 2 is the median.

View Solution →## Minimum Average Waiting Time

Tieu owns a pizza restaurant and he manages it in his own way. While in a normal restaurant, a customer is served by following the first-come, first-served rule, Tieu simply minimizes the average waiting time of his customers. So he gets to decide who is served first, regardless of how sooner or later a person comes. Different kinds of pizzas take different amounts of time to cook. Also, once h

View Solution →## Merging Communities

People connect with each other in a social network. A connection between Person I and Person J is represented as . When two persons belonging to different communities connect, the net effect is the merger of both communities which I and J belongs to. At the beginning, there are N people representing N communities. Suppose person 1 and 2 connected and later 2 and 3 connected, then ,1 , 2 and 3 w

View Solution →## Components in a graph

There are 2 * N nodes in an undirected graph, and a number of edges connecting some nodes. In each edge, the first value will be between 1 and N, inclusive. The second node will be between N + 1 and , 2 * N inclusive. Given a list of edges, determine the size of the smallest and largest connected components that have or more nodes. A node can have any number of connections. The highest node valu

View Solution →## Kundu and Tree

Kundu is true tree lover. Tree is a connected graph having N vertices and N-1 edges. Today when he got a tree, he colored each edge with one of either red(r) or black(b) color. He is interested in knowing how many triplets(a,b,c) of vertices are there , such that, there is atleast one edge having red color on all the three paths i.e. from vertex a to b, vertex b to c and vertex c to a . Note that

View Solution →## Super Maximum Cost Queries

Victoria has a tree, T , consisting of N nodes numbered from 1 to N. Each edge from node Ui to Vi in tree T has an integer weight, Wi. Let's define the cost, C, of a path from some node X to some other node Y as the maximum weight ( W ) for any edge in the unique path from node X to Y node . Victoria wants your help processing Q queries on tree T, where each query contains 2 integers, L and

View Solution →