2402021-03-05 15:16:07balintttÓvodacpp14Partially correct 35/50104ms12012 KiB
#include<bits/stdc++.h>

using namespace std;

class gyerek{
public:
    int sz, e;
    int index;
    int t=-1;
};

int n, m;

vector<int> mx;
vector<int> db;

vector<gyerek> p;

bool operator<(const gyerek& g1, const gyerek& g2){
    return g1.e > g2.e;
}

bool cmp1(const gyerek& g1, const gyerek& g2){
    return g1.index < g2.index;
}


int main(){
    cin >> n >> m;
    mx.resize(m);
    db.resize(m);
    for(int i=0;i<m;i++){
        cin >> mx[i];
    }
    p.resize(n);
    for(int i=0;i<n;i++){
        cin >> p[i].sz;
        --p[i].sz;
        p[i].index = i;
    }
    for(int i=0;i<n;i++){
        cin >> p[i].e;
        p[i].index = i;
    }
    sort(p.begin(), p.end());
    stack<int> ava;
    for(int i=0;i<n;i++){
        if(db[p[i].sz] < mx[p[i].sz]){
            p[i].t = p[i].sz;
            ++db[p[i].sz];
        } else {
            ava.push(i);
        }
    }
    int poi = n-1;
    for(int i = 0;i<m;i++){
        if(db[i] == 0){
            if(ava.size()){
                p[ava.top()].t = i;
                ++db[i];
                ava.pop();
            } else {
                while(db[p[poi].t] <2){
                    --poi;
                }
                --db[p[poi].t];
                p[poi].t = i;
                ++db[i];
            }
        }
    }
    sort(p.begin(), p.end(), cmp1);
    long long er=0;
    for(int i=0;i<n;i++){
        //cout << p[i].t << ' ';
        er+=(p[i].t!=p[i].sz)*p[i].e;
    }
    cout << er << '\n';
    for(int i=0;i<n;i++){
        cout << p[i].t+1 << ' ';
        //er+=(p[i].t!=p[i].sz)*p[i].e;
    }
return 0;
}
SubtaskSumTestVerdictTimeMemory
base35/50
1Accepted0/02ms1800 KiB
2Accepted0/07ms2012 KiB
3Partially correct1/22ms1884 KiB
4Accepted2/22ms1784 KiB
5Partially correct1/21ms1896 KiB
6Accepted2/21ms1892 KiB
7Accepted2/21ms1896 KiB
8Partially correct1/21ms1900 KiB
9Accepted2/21ms1912 KiB
10Accepted2/21ms1916 KiB
11Partially correct1/21ms1912 KiB
12Partially correct1/21ms1916 KiB
13Accepted2/22ms1940 KiB
14Partially correct1/32ms1964 KiB
15Accepted3/312ms2420 KiB
16Accepted3/334ms3316 KiB
17Partially correct1/339ms4148 KiB
18Accepted3/361ms6008 KiB
19Partially correct1/370ms6856 KiB
20Partially correct1/383ms8232 KiB
21Partially correct1/389ms9600 KiB
22Accepted4/4104ms12012 KiB