CodeForces Cards and Joy (dp)

There are nn players sitting at the card table. Each player has a favorite number. The favorite number of the jj-th player is fjfj.

There are k⋅nk⋅n cards on the table. Each card contains a single integer: the ii-th card contains number cici. Also, you are given a sequence h1,h2,…,hkh1,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 kk cards. After all the cards are distributed, each player counts the number of cards he has that contains his favorite number. The joy level of a player equals htht if the player holds tt cards containing his favorite number. If a player gets no cards with his favorite number (i.e., t=0t=0), his joy level is 00.

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

Input

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

The second line contains k⋅nk⋅n integers c1,c2,…,ck⋅nc1,c2,…,ck⋅n (1≤ci≤1051≤ci≤105) — the numbers written on the cards.

The third line contains nn integers f1,f2,…,fnf1,f2,…,fn (1≤fj≤1051≤fj≤105) — the favorite numbers of the players.

The fourth line contains kk integers h1,h2,…,hkh1,h2,…,hk (1≤ht≤1051≤ht≤105), where htht is the joy level of a player if he gets exactly tt cards with his favorite number written on them. It is guaranteed that the condition ht−1<htht−1<ht holds for each t∈[2..k]t∈[2..k].

Output

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

Examples

Input

4 3
1 3 2 8 5 5 8 2 2 8 5 2
1 2 2 5
2 6 7

Output

21

Input

3 3
9 9 9 9 9 9 9 9 9
1 2 3
1 2 3

Output

0

Note

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

  • Player 11 gets cards with numbers [1,3,8][1,3,8];
  • Player 22 gets cards with numbers [2,2,8][2,2,8];
  • Player 33 gets cards with numbers [2,2,8][2,2,8];
  • Player 44 gets cards with numbers [5,5,5][5,5,5].

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

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

题意:n个人,每人要发k张卡片,每张卡片上都有一个数字,每个人都有喜欢的一个数字,若有抽到的三个数中有,i个是他喜欢的,那么将会得到h[i]的分数,n个人最大的分数和是多少?

思路:首先我们应该先统计出每种相同的数字有多少个,对每种有人喜欢的数字,我们去考虑如何给这n个人分配,使得他们的分数最大。

dp[i][j]表示i张相同的卡片分给j个人得到的最大值,(这j个人都喜欢这张卡片对应的数字)

考虑转移方程:dp[i][j]=max(dp[i][j],dp[i-u][j-1]+h[u])(u>=1&&u<=min(k,i)),意思是,当有i张卡片和j个人时,我们考虑先给一个人分u张卡片,剩下的人分i-u张卡片。

枚举出所有可能性。

最后,我们还需要累计喜欢各种卡片的数人对应的dp值.

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
#include <set>
#include <string>
#include <math.h>
#include <stack>
typedef long long ll;
using namespace std;
const int maxn =0x3f3f3f3f;
const int INF=1e9;
int n, k, c[5005], f[505], h[15];
int cnt[100005], num[100005], dp[5005][505];
int main(int argc, char const *argv[])
{
    scanf("%d%d", &n, &k);
    for (int i = 0; i < n*k; i++) 
    {
        scanf("%d", &c[i]), cnt[c[i]]++;
    }
    for (int i = 0; i < n; i++)
    {
        scanf("%d", &f[i]), num[f[i]]++;
    }
    for (int i = 1; i <= k; i++) 
    {
        scanf("%d", &h[i]);
    }
    for(int i=1;i<=n*k;i++)
    {
        dp[i][1]=h[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]+h[u]);
            }
        }
    }
    ll ans = 0;
    for (int i = 0; i <= 1e5; i++)
    {
        if (num[i])
        {
            ans += dp[cnt[i]][num[i]];
        }
    }
    printf("%lld\n", ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40774175/article/details/81321072