Description
为了备考NHOI,小明需要学习 m 种算法。一开始小明每种算法的水平都是 0,完全是零基础。
书店有 n 本书,第 i 本书的价格是 c[i]。
如果小明买了第 i 本书并阅读该书,那么小明的所有 m 种算法的水平都有所提升,其中第 j 种算法水平会提升 s[i][j]。
为了确保在NHOI比赛能获得一等奖,每一种算法的水平都至少要达到 X。
小明可以获得一等奖吗?如果可以,输出最少的购书费用;如果不行,输出 -1。
Format
Input
第一行,三个整数:n,m,X。1 ≤ n,m ≤ 12,1 ≤ X ≤ 100000。
接下来有 n 行,每行描述一本书的相关信息,格式如下:
c[1] s[1][1] s[1][2] s[1][3]......s[1][m]
c[2] s[2][1] s[2][2] s[2][3]......s[2][m]
......
c[n] s[n][1] s[n][2] s[n][3]......s[n][m]
其中 1 ≤ c[i] ≤ 100000。
Output
一个整数。
Samples
输入数据 1
3 3 10
60 2 2 4
70 8 7 9
50 2 3 9
输出数据 1
120
输入数据 2
3 3 10
100 3 1 4
100 1 5 9
100 2 6 5
输出数据 2
-1
代码:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=1e6+10;
const int INF=0x3f3f3f3f;
inline int read(){int x=0,f=1;char ch=getchar();while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}return x*f;}
int n,m,x,c[N],s[1005][1005],f,minn=INF,ans,sf[N];
int main(){
cin>>n>>m>>x;
for(int i=1;i<=n;i++){
cin>>c[i];
for(int j=1;j<=m;j++)cin>>s[i][j];
}
for(int i=0;i<(1<<n);i++){
f=0;ans=0;
memset(sf,0,sizeof(sf));
int t=i;
for(int j=n;j>=1;j--){
if(t&1){
ans+=c[j];
for(int k=1;k<=m;k++){
sf[k]+=s[j][k];
}
}
t>>=1;
}
for(int j=1;j<=m;j++){
if(sf[j]<x){
f=1;
break;
}
}
if(!f){
minn=min(minn,ans);
}
}
if(minn==INF)cout<<-1,exit(0);
else cout<<minn;
}