241802026-02-05 17:36:56ubormaciÓvodacpp17Accepted 50/5075ms12684 KiB
#include <iostream>
#include <algorithm> // for sort, mainly
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <array>
#include <string>
#include <cstdio>
#include <iterator>
#include <unordered_set>
#include <cstdint> // for int64_t, int32_t, etc
#include <queue>
#include <stack>
#include <deque>
#include <numeric> // gcd, lcm
#include <fstream>
#include <bitset> // for bitset
#include <iomanip>
#include <cassert> // for set with custom ordering
#include <type_traits> // for set with custom ordering
#include <utility> // for swap, forward, etc
using namespace std;

#pragma GCC optimize("O2")
// #pragma GCC optimize("O1","O2","O3","Ofast","unroll-loops")
// #pragma GCC target("sse","sse2","sse3","sse4.1","sse4.2","avx","avx2","fma")

template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cout << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cout << ' ' << H; dbg_out(T...); }
#ifdef LOCAL
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif

/*

notes:

int64_t
stoi(string s) -> string to int
to_string() -> int (or else) to string

vector declaration:
vector<ll> v(n, 0)
vector<vector<ll>> v(n, vector<ll>(n, 0));

{if statement} ? {truth value} : {false value}

#ifdef LOCAL
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
#endif

std::lcm(ll a, ll b), std::gcd(int a, int b)

cout << fixed << setprecision(n);

set with custom ordering
set<ll, decltype(&cmp)> qu(cmp);

*/

typedef int64_t ll;

void solve() {

	ll n, k;
	cin >> n >> k;

	vector<ll> ans(n, 0);

	vector<ll> cnt(k, 0);
	for(ll i = 0; i < k; i++) {
		cin >> cnt[i];
	}

	vector<ll> want(n, 0);
	vector<ll> cry(n, 0);

	vector<vector<pair<ll,ll>>> here(n);
	// cry, ind

	for(ll i = 0; i < n; i++) {
		cin >> want[i];
		want[i]--;
	}
	for(ll i = 0; i < n; i++) {
		cin >> cry[i];
		here[want[i]].push_back({cry[i], i});
	}

	vector<ll> nd; // need <- positions that would remain empty
	vector<pair<ll,ll>> extra;
	vector<pair<ll,ll>> potextra; // penalty, place
	for(ll i = 0; i < k; i++) {

		if(here[i].size() == 0) {
			nd.push_back(i);
		}else{

			sort(here[i].rbegin(), here[i].rend());

			// let's see how many we can place here
			// any that are left over can be used to cover the extras

			// so
			// the worst cnt[i] get the places they want
			// then the others are extras
			// however, everyone besides the literal worst is added as a "potential" extra

			ans[here[i][0].second] = i;

			for(ll j = 1; j < min((ll)here[i].size(), cnt[i]); j++) {
				ans[here[i][j].second] = i;
				potextra.push_back(here[i][j]);
			}

			for(ll j = cnt[i]; j < here[i].size(); j++) {
				extra.push_back(here[i][j]);
			}
		}	
	}

	sort(extra.begin(), extra.end());
	sort(potextra.begin(), potextra.end());

	//cerr << "\nextra=" << extra;
	//cerr << "\npotextra=" << potextra;

	ll exi = 0;
	ll pxi = 0;
	ll pen = 0;

	for(const auto & h : nd) {
		pair<ll,ll> p;

		if(exi < extra.size()) {
			// take from extra
			p = extra[exi];
			exi++;
		}else{
			// take from potextra
			p = potextra[pxi];
			pxi++;
		}

		pen += p.first;
		ans[p.second] = h;
	}

	//cerr << "\nans after nd=" << ans;

	vector<pair<ll,ll>> freeplace;

	for(ll i = 0; i < k; i++) {

		ll free = cnt[i] - here[i].size();
		if(here[i].size() == 0) {
			free = cnt[i] - 1;
		}

		if(free > 0) {
			freeplace.push_back({free, i});
		}
	}

	//cerr << "\nfreeplace=" << freeplace;

	ll wh = 0;
	for(ll j = exi; j < extra.size(); j++) {
		// az extrakat muszaj mashol elhelyezzuk
		// megnezzuk, az adott pozicion van-e szabad hely
		// ha igen, direkt berakjuk oda
		// nem fontos

		//cerr << "\nj=" << extra[j];

		while(freeplace[wh].first <= 0) {
			wh++;
		}

		ans[extra[j].second] = freeplace[wh].second;
		freeplace[wh].first--;
		pen += extra[j].first;
	}

	cout << pen << "\n";
	//cerr << "\nans=" << ans;
	for(ll i = 0; i < n; i++) {
		cout << ans[i]+1 << " ";
	}

}

int main()
{
	std::ios_base::sync_with_stdio(false);
	//cin.tie(nullptr);
	//cout.tie(nullptr);

	solve();

	return 0;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/01ms316 KiB
2Accepted0/04ms820 KiB
3Accepted2/21ms500 KiB
4Accepted2/21ms316 KiB
5Accepted2/21ms316 KiB
6Accepted2/21ms316 KiB
7Accepted2/21ms332 KiB
8Accepted2/21ms316 KiB
9Accepted2/21ms316 KiB
10Accepted2/21ms316 KiB
11Accepted2/21ms316 KiB
12Accepted2/21ms316 KiB
13Accepted2/22ms316 KiB
14Accepted3/32ms564 KiB
15Accepted3/38ms1588 KiB
16Accepted3/317ms2940 KiB
17Accepted3/328ms4780 KiB
18Accepted3/346ms7292 KiB
19Accepted3/350ms8584 KiB
20Accepted3/359ms10504 KiB
21Accepted3/364ms10164 KiB
22Accepted4/475ms12684 KiB