CodeForces 999F Cards and Joy 动态规划

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

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 knk⋅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 (1n500,1k101≤n≤500,1≤k≤10) — the number of players and the number of cards each player will get.

The second line contains knk⋅n integers c1,c2,,cknc1,c2,…,ck⋅n (1ci1051≤ci≤105) — the numbers written on the cards.

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

The fourth line contains kk integers h1,h2,,hkh1,h2,…,hk (1ht1051≤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 ht1<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
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 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.


我又被动态规划干倒了!

f[i][j]代表一共i张牌发给j个人能取得的最大价值。


#include<iostream>
#include<cstdio>
#include<string.h>
#include<algorithm>
#include<cmath>
using namespace std;

int n,k;
long long c[5010],a[505],h[15],f[5010][505],people[100010],card[100010];

int main()
{
    scanf("%d%d",&n,&k);
    memset(people,0,sizeof(people));
    memset(card,0,sizeof(card));
    for (int i=1; i<=n*k; i++)
    {
        scanf("%lld",&c[i]);
        card[c[i]]++;
    }
    for (int i=1; i<=n; i++)
    {
        scanf("%lld",&a[i]);
        people[a[i]]++;
    }
    for (int i=1; i<=k; i++)
    {
        scanf("%lld",&h[i]);
    }
    memset(f,0,sizeof(f));
    for (int i=1; i<=n*k; i++)
    {
        f[i][1]=h[min(i,k)];
        for (int j=2; j<=n; j++)
        {
            for (int u=1; u<=min(i,k); u++)
            {
                f[i][j]=max(f[i][j],f[i-u][j-1]+h[u]);
            }
        }
    }
    long long ans=0;
    for (int i=0; i<=1e5; i++)
    if (people[i])
        ans+=f[card[i]][people[i]];
    cout<<ans<<endl;
    return 0;
}


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值