CodeCraft-20 (Div. 2) E. Team Building(状压dp)

题目传送门

E. Team Building

time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Alice, the president of club FCB, wants to build a team for the new volleyball tournament. The team should consist of pp players playing in pp different positions. She also recognizes the importance of audience support, so she wants to select kk people as part of the audience.

There are nn people in Byteland. Alice needs to select exactly pp players, one for each position, and exactly kk members of the audience from this pool of nn people. Her ultimate goal is to maximize the total strength of the club.

The ii-th of the nn persons has an integer aiai associated with him — the strength he adds to the club if he is selected as a member of the audience.

For each person ii and for each position jj, Alice knows si,jsi,j  — the strength added by the ii-th person to the club if he is selected to play in the jj-th position.

Each person can be selected at most once as a player or a member of the audience. You have to choose exactly one player for each position.

Since Alice is busy, she needs you to help her find the maximum possible strength of the club that can be achieved by an optimal choice of players and the audience.

Input

The first line contains 33 integers n,p,kn,p,k (2≤n≤105,1≤p≤7,1≤k,p+k≤n2≤n≤105,1≤p≤7,1≤k,p+k≤n).

The second line contains nn integers a1,a2,…,ana1,a2,…,an. (1≤ai≤1091≤ai≤109).

The ii-th of the next nn lines contains pp integers si,1,si,2,…,si,psi,1,si,2,…,si,p. (1≤si,j≤1091≤si,j≤109)

Output

Print a single integer resres  — the maximum possible strength of the club.

Examples

input

Copy

4 1 2
1 16 10 3
18
19
13
15

output

Copy

44

input

Copy

6 2 3
78 93 9 17 13 78
80 97
30 52
26 17
56 68
60 36
84 55

output

Copy

377

input

Copy

3 2 1
500 498 564
100002 3
422332 2
232323 1

output

Copy

422899

Note

In the first sample, we can select person 11 to play in the 11-st position and persons 22 and 33 as audience members. Then the total strength of the club will be equal to a2+a3+s1,1a2+a3+s1,1.

题意:

给你n(<=1e5)个人,这场比赛有p(<=7)个位置,k(<=n,k+p<=n)个观众,每个人当观众的价值ai,再给你每个人参加第j个位置的价值sij。一个位置有且只有一个人,不能同时又当观众又选位置。求最大的价值。

思路:对p个位置可以进行状压dp,但是又要保证价值最大,于是我们把所有人按ai从大到小排序,这样dp的时候,假设当前p个位置的状态为zt,x=count(zt)为zt的二进制中1的个数,那么i-x<=k时我们就直接选他当观众。否则不选。

仔细想一下,其实并不难。注意dp数组初始化为-inf,dp[0][0]=0

代码:

#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
#define rep(i,a,b) for(register int i=(a);i<=(b);i++)
#define dep(i,a,b) for(register int i=(a);i>=(b);i--)
using namespace std;
const int maxn=2e5+5;
//const double pi=acos(-1.0);
//const double eps=1e-9;
//const ll mo=1e9+7;
int n,m,p,k;
int aa[maxn];
int ans,tmp,cnt;
int flag;
char s[maxn];
struct node
{
    int val;
    int c[8];
    bool operator<(node aa)const{
        return val>aa.val;
    }
}a[maxn];
ll dp[maxn][(1<<7)+5];
int count(int zt){
    int sum=0;
    while(zt){zt&=(zt-1);sum++;}
    return sum;
}
int main(){
    int T,cas=1;
    //scanf("%d",&T);
    //while(T--)
    {
        scanf("%d%d%d",&n,&p,&k);
        rep(i,1,n) scanf("%d",&a[i].val);
        rep(i,1,n)
        rep(j,0,p-1) scanf("%d",&a[i].c[j]);
        sort(a+1,a+n+1);
        int m=(1<<p)-1;
        rep(i,0,n) rep(j,0,m) dp[i][j]=-inf;
        dp[0][0]=0;
        rep(i,1,n){
            rep(j,0,m){
               if(count(j)>i) continue;
               dp[i][j]=dp[i-1][j];
               if(i-count(j)<=k) dp[i][j]+=a[i].val;
               rep(t,0,p-1)
               if(j&(1<<t)){
                   dp[i][j]=max(dp[i][j],dp[i-1][j^(1<<t)]+a[i].c[t]);
               }
            }
        }
        printf("%lld\n",dp[n][m]);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值