// ovoda.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
//#include <fstream>
#include <iostream>
#include <vector>
#include <deque>
#include <algorithm>
#define int long long
using namespace std;
//ifstream fin("ovoda.in");
int i, n, m, ures, szerep, sir, j, last;
deque <int> maradek;
struct adat1
{
int fer;
bool van=0;
};
struct adat2
{
int konny, cel, poz, lett;
};
bool comp1 (adat2 a, adat2 b)
{
return a.konny > b.konny;
}
bool comp2(adat2 a, adat2 b)
{
return a.poz < b.poz;
}
signed main()
{
cin >> n >> m;
vector <adat1> szin(m + 1);
vector <adat2> gyerek(n + 1);
for (i = 1; i <= m; ++i)
{
cin >> szin[i].fer;
}
for (i = 1; i <= n; ++i)
{
cin >> gyerek[i].cel;
gyerek[i].poz = i;
}
for (i = 1; i <= n; ++i)
{
cin >> gyerek[i].konny;
}
sort(gyerek.begin() + 1, gyerek.end(), comp1);
ures = m;
for (i = 1; i <= n; ++i)
{
szerep = gyerek[i].cel;
if (szin[szerep].fer)
{
if (!szin[szerep].van)
{
szin[szerep].van = 1;
}
gyerek[i].lett = szerep;
--szin[szerep].fer;
}
else
{
maradek.push_back(i);
sir += gyerek[i].konny;
}
}
last = n;
for (i = 1; i <= m; ++i)
{
if (!szin[i].van)
{
if (!maradek.empty())
{
szin[i].van = 1;
--szin[i].fer;
gyerek[maradek.front()].lett = i;
maradek.pop_front();
}
else
{
for (j = last; j >= 1; --j)
{
if (gyerek[j].lett == gyerek[j].cel)
{
szin[i].van = 1;
--szin[i].fer;
gyerek[j].lett = i;
last = j-1;
break;
}
}
}
}
}
i = 1;
while (!maradek.empty() && i<=m)
{
while (!maradek.empty() && szin[i].fer)
{
--szin[i].fer;
gyerek[maradek.front()].lett = i;
maradek.pop_front();
}
++i;
}
sort(gyerek.begin() + 1, gyerek.end(), comp2);
cout << sir << "\n";
for (i = 1; i <= n; ++i)
{
cout << gyerek[i].lett << " ";
}
return 0;
}
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu
// Tips for Getting Started:
// 1. Use the Solution Explorer window to add/manage files
// 2. Use the Team Explorer window to connect to source control
// 3. Use the Output window to see build output and other messages
// 4. Use the Error List window to view errors
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file