## Cards and Joy CodeForces - 999F （贪心+set）

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
4 31 3 2 8 5 5 8 2 2 8 5 21 2 2 52 6 7
Output
21
Input
3 39 9 9 9 9 9 9 9 91 2 31 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].

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

x=a[i]%m

#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=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
inline void getInt(int* p);
const int maxn=1000010;
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,1,n)
{
cin>>a[i];
}
set<int> st;
repd(i,0,m-1)
{
st.insert(i);
}
ll x;
ll y;
ll ans=0ll;
repd(i,1,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,1,n)
{
cout<<a[i]<<" ";
}
cout<<endl;
return 0;
}

inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '0');
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 - ch + '0';
}
}
else {
*p = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 + ch - '0';
}
}
}

0条评论