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;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章