F. Cards and Joy
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

There are fj.

There are h1,h2,…,hk. Its meaning will be explained below.

The players have to distribute all the cards in such a way that each of them will hold exactly 0.

Print the maximum possible total joy levels of the players after the cards are distributed. Note that the sequence h1,…,hk is the same for all the players.

Input

The first line of input contains two integers 1≤n≤500,1≤k≤10) — the number of players and the number of cards each player will get.

The second line contains 1≤ci≤105) — the numbers written on the cards.

The third line contains 1≤fj≤105) — the favorite numbers of the players.

The fourth line contains t∈[2..k].

Output

Print one integer — the maximum possible total joy levels of the players among all possible card distributions.

Examples
input
Copy
4 3
1 3 2 8 5 5 8 2 2 8 5 2
1 2 2 5
2 6 7
output
Copy
21
input
Copy
3 3
9 9 9 9 9 9 9 9 9
1 2 3
1 2 3
output
Copy
0
Note

In the first example, one possible optimal card distribution is the following:

  • Player [1,3,8];
  • Player [2,2,8];
  • Player [2,2,8];
  • Player [5,5,5].

Thus, the answer is 2+6+6+7=21.

In the second example, no player can get a card with his favorite number. Thus, the answer is 0.


题意:

题解:DP,dp[i][j],表示i个相同的数字给j个人(这j个人都喜欢这相同的数字);cnt[i]表示数字i的数量,num[j]表示喜欢j 的人数;

状态转换方程为:dp[i][j]=max(dp[i][j],dp[i-u][j-1]+w[u]);(1=<u<=k)

AC代码为:

#include<bits/stdc++.h>
using namespace std;


const int maxn=1e5+10;
int n,k,a[maxn],f[5005],w[15];
int cnt[maxn],num[maxn],dp[5005][521];


int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    memset(cnt,0,sizeof cnt);
    memset(num,0,sizeof num);
    cin>>n>>k;
    for(int i=1;i<=n*k;i++) cin>>a[i],cnt[a[i]]++;
    for(int i=1;i<=n;i++) cin>>f[i],num[f[i]]++;
    for(int i=1;i<=k;i++) cin>>w[i];
    
    for(int i=1;i<=n*k;i++)
    {
        dp[i][1]=w[min(i,k)];
        for(int j=2;j<=n;j++)
        {
            for(int u=1;u<=min(i,k);u++) 
                dp[i][j]=max(dp[i][j],dp[i-u][j-1]+w[u]);
        }
    }
    long long ans=0;
    for(int i=1;i<maxn;i++) if(num[i]) ans+=dp[cnt[i]][num[i]];
    cout<<ans<<endl;
    
    return 0;
}


相关文章: