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个数的数组,还有一个数m,m一定是n的因子,。

现在你可以改变数组中的每一个数,使之n个数对m取模后的结果值的数量严格为n/m

让一个数+1的成本是1,问最小的改变成本是多少?

思路:

巧妙的运用了set的功能和贪心的思想。

先把0~m-1的所有数加入到set中,

然后扫一遍数组,对于每一个a[i],

x=a[i]%m

然后我们就要从set中找到那个让a[i]增加值最小的那个取模后的数,

如果x比set中所有的数大,那么我们必须让它加一点数然后变成%m后是set中最小数才可以成本最低。

否则我们需要用到set中的一个函数lower_bound()

这个函数的作用想必都知道,就说在set中找到第一个大于等于x的数。

注意函数返回的是一个set的迭代器,*iterator 才是取值。

然后把a[i]进行改变,最后输出答案。

细节见代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=;while(b){if(b%)ans=ans*a%MOD;a=a*a%MOD;b/=;}return ans;}
inline void getInt(int* p);
const int maxn=;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
ll n,m;
ll a[maxn];
ll b[maxn];
ll c[maxn];
int main()
{
//freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
//freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);
gbtb;
cin>>n>>m;
ll num=n/m;
repd(i,,n)
{
cin>>a[i];
}
set<int> st;
repd(i,,m-)
{
st.insert(i);
}
ll x;
ll y;
ll ans=0ll;
repd(i,,n)
{
x=a[i]%m;
if(x>(*st.rbegin()))
{
y=*st.begin();
}else
{
y=*st.lower_bound(x);
}
b[y]++;
if(b[y]==num)
{
st.erase(y);
}
a[i]+=((y-x)+m)%m;
ans+=((y-x)+m)%m;
}
cout<<ans<<endl;
repd(i,,n)
{
cout<<a[i]<<" ";
}
cout<<endl;
return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}

Cards and Joy CodeForces - 999F (贪心+set)的更多相关文章

  1. Codeforces Round #490 (Div. 3) F - Cards and Joy

    F - Cards and Joy 思路:比较容易想到dp,直接dp感觉有点难,我们发现对于每一种数字要处理的情况都相同就是有 i 张牌 要给 j 个人分, 那么我们定义dp[ i ][ j ]表示 ...

  2. F. Cards and Joy

    F. Cards and Joy 题目大意: 给你n个人,每一个人恰好选k张牌. 第一行是 n 和 k 第二行有n*k个数,代表有n*k张牌,每张牌上的数字 第三行有n个数,代表第i个人喜欢的数字 第 ...

  3. Codeforces 999F Cards and Joy(二维DP)

    题目链接:http://codeforces.com/problemset/problem/999/F 题目大意:有n个人,n*k张卡牌,每个人会发到k张卡牌,每个人都有一种喜欢的卡牌f[i],当一个 ...

  4. Codeforces Round #490 (Div. 3) :F. Cards and Joy(组合背包)

    题目连接:http://codeforces.com/contest/999/problem/F 解题心得: 题意说的很复杂,就是n个人玩游戏,每个人可以得到k张卡片,每个卡片上有一个数字,每个人有一 ...

  5. 999F Cards and Joy

    传送门 题目大意 有n个人n*m张牌,每个人分m张牌.每个人有一个自己喜欢的数值,如果他的牌中有x张数值等于这个值则他的高兴度为L[x],求怎样分配牌可以使得所有人的总高兴度最大. 分析 我们发现每一 ...

  6. CodeForces - 893D 贪心

    http://codeforces.com/problemset/problem/893/D 题意 Recenlty Luba有一张信用卡可用,一开始金额为0,每天早上可以去充任意数量的钱.到了晚上, ...

  7. Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 831D) - 贪心 - 二分答案 - 动态规划

    There are n people and k keys on a straight line. Every person wants to get to the office which is l ...

  8. Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 828D) - 贪心

    Arkady needs your help again! This time he decided to build his own high-speed Internet exchange poi ...

  9. CodeForces - 93B(贪心+vector<pair<int,double> >+double 的精度操作

    题目链接:http://codeforces.com/problemset/problem/93/B B. End of Exams time limit per test 1 second memo ...

随机推荐

  1. 如何将html特殊字符编码转换成特殊字符_html十进制编码字符转回来

    备注:有时候我们会莫名其妙遇到一些特殊字符:  这些字符在网页上能正常显示,但是在APP特殊情景并不识别这些字符: 如:'     这个其实是单引号:   '     百度后发现,它其实是HTML特殊 ...

  2. Oracle 连接 另一个Oracle数据库 服务器连接

    一.场景   两台不同的服务器A.B分别装有不同业务的oracle数据库,因业务需要,现需要将B中test表的数据,定时同步到A中. 二.实现   根据以上场景,我想到了oracle中的dblink, ...

  3. Dynamics 365使用Execute Multiple Request删除系统作业实体记录

    摘要: 本人微信公众号:微软动态CRM专家罗勇 ,回复295或者20190112可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me!我的网站是 www.luoyong.me ...

  4. ps -ef |grep java

    一.ps -ef |grep java 查看包含“java”的所有进程 二.涉及命令详解 ps命令将某个进程显示出来(是LINUX下最常用的也是非常强大的进程查看命令) grep命令是查找(是一种强大 ...

  5. Java 8 的Lambda表达式

    第一次看到用lambda表达式写出来的Java代码时,我对这种神秘的语法感到非常不理解,认为这样的Java搞得不可读.没有专门的去学习和认识,今天花了几个小时写了示例的练习后,我开心的看到了更清晰的J ...

  6. Docker 创建 Confluence6.12.2 中文版

    目录 目录 1.介绍 1.1.什么是Confluence? 2.Confluence的官网在哪里? 3.如何下载安装? 4.对 Confluence 进行配置 4.1.设置 Confluence 4. ...

  7. 测者的性能测试手册:JVM的监控利器

    测者的性能测试手册:JVM的监控利器 每次聊起性能测试,最后的终结话题就是怎么做优化.其实在Java的复杂项目中都会有内存不足问题.内存泄露问题.线程死锁问题.CPU问题.这些问题工程测试或者是小压力 ...

  8. git rebase 使用详解

    rebase 假设你现在基于远程分支"origin",创建一个叫"mywork"的分支. $ git checkout -b mywork origin   现 ...

  9. C#中Windows窗体工具栏

    标签(Label):用于显示用户不能编辑的文本或图像.常使用它对窗体上的其他各种控件进行标注或说明. 文本框(TextBox):用于获取用户名输入的信息或向用户显示的文本. 组合框(ComboBox) ...

  10. eclipse IDE使用git方法简单介绍

    eclipse下使用git插件上传代码至github 1.eclipse下安装git eclipse  git 插件的安装. 点击 Help->Install New Software-> ...