9282022-01-28 14:59:14Valaki2Óvodacpp14Accepted 50/5064ms20356 KiB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define pb push_back
#define mp make_pair
#define fi first
#define se second

int n, k;
vector<int> limit;
vector<int> wanted;
vector<int> cry;

vector<vector<int> > v;

vector<int> ans;

bool sortbycry(int a, int b) {
    return cry[a] < cry[b];
}

void solve() {
    cin >> n >> k;
    limit.assign(1 + k, 0);
    wanted.assign(1 + n, 0);
    cry.assign(1 + n, 0);
    for(int i = 1; i <= k; i++) {
        cin >> limit[i];
    }
    for(int i = 1; i <= n; i++) {
        cin >> wanted[i];
    }
    for(int i = 1; i <= n; i++) {
        cin >> cry[i];
    }
    v.assign(1 + n, vector<int> (0, 0));
    ans = wanted;
    for(int i = 1; i <= n; i++) {
        v[wanted[i]].pb(i);
    }
    vector<int> changed;
    for(int i = 1; i <= k; i++) {
        sort(v[i].begin(), v[i].end(), sortbycry);
        reverse(v[i].begin(), v[i].end());
        if(limit[i] < (int) v[i].size()) {
            for(int j = limit[i]; j < (int) v[i].size(); j++) {
                changed.pb(v[i][j]);
            }
            v[i].resize(limit[i]);
        }
    }
    int idx = 0, cnt = 0;
    vector<int> unused;
    for(int i = 1; i <= k; i++) {
        if(((int) v[i].size()) == 0) {
            if(idx < (int) changed.size()) {
                ans[changed[idx]] = i;
                v[i].pb(changed[idx]);
                idx++;
            } else {
                cnt++;
                unused.pb(i);
            }
        }
    }
    if(idx < (int) changed.size()) {
        for(int i = 1; i <= k; i++) {
            while((limit[i] > (int) v[i].size()) && (idx < (int) changed.size())) {
                ans[changed[idx]] = i;
                v[i].pb(changed[idx]);
                idx++;
            }
        }
    } else if(cnt > 0) {
        vector<int> options;
        for(int i = 1; i <= k; i++) {
            for(int j = 1; j < (int) v[i].size(); j++) {
                options.pb(v[i][j]);
            }
        }
        sort(options.begin(), options.end(), sortbycry);
        for(int i = 0; i < cnt; i++) {
            ans[options[i]] = unused[i];
        }
    }
    int ans_cry = 0;
    for(int i = 1; i <= n; i++) {
        if(ans[i] != wanted[i]) {
            ans_cry += cry[i];
        }
    }
    cout << ans_cry << "\n";
    for(int i = 1; i <= n; i++) {
        cout << ans[i] << " ";
    }
    cout << "\n";
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
    return 0;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/02ms1820 KiB
2Accepted0/04ms2296 KiB
3Accepted2/21ms1912 KiB
4Accepted2/21ms1916 KiB
5Accepted2/21ms1920 KiB
6Accepted2/21ms1920 KiB
7Accepted2/21ms1928 KiB
8Accepted2/21ms1952 KiB
9Accepted2/21ms1956 KiB
10Accepted2/21ms1960 KiB
11Accepted2/21ms1948 KiB
12Accepted2/21ms1948 KiB
13Accepted2/22ms2012 KiB
14Accepted3/31ms2028 KiB
15Accepted3/37ms3464 KiB
16Accepted3/316ms5864 KiB
17Accepted3/318ms6812 KiB
18Accepted3/339ms10760 KiB
19Accepted3/332ms11524 KiB
20Accepted3/339ms14024 KiB
21Accepted3/345ms15424 KiB
22Accepted4/464ms20356 KiB