9282022-01-28 14:59:14Valaki2Óvodacpp14Elfogadva 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;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base50/50
1Elfogadva0/02ms1820 KiB
2Elfogadva0/04ms2296 KiB
3Elfogadva2/21ms1912 KiB
4Elfogadva2/21ms1916 KiB
5Elfogadva2/21ms1920 KiB
6Elfogadva2/21ms1920 KiB
7Elfogadva2/21ms1928 KiB
8Elfogadva2/21ms1952 KiB
9Elfogadva2/21ms1956 KiB
10Elfogadva2/21ms1960 KiB
11Elfogadva2/21ms1948 KiB
12Elfogadva2/21ms1948 KiB
13Elfogadva2/22ms2012 KiB
14Elfogadva3/31ms2028 KiB
15Elfogadva3/37ms3464 KiB
16Elfogadva3/316ms5864 KiB
17Elfogadva3/318ms6812 KiB
18Elfogadva3/339ms10760 KiB
19Elfogadva3/332ms11524 KiB
20Elfogadva3/339ms14024 KiB
21Elfogadva3/345ms15424 KiB
22Elfogadva4/464ms20356 KiB