40522023-03-11 09:55:201478Óvodacpp17Partially correct 40/5064ms20880 KiB
#include <bits/stdc++.h>
#include <random>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> p32;
typedef pair<ll, ll> p64;
typedef pair<double, double> pdd;
typedef vector<ll> v64;
typedef vector<int> v32;
typedef vector<vector<int>> vv32;
typedef vector<vector<ll>> vv64;
typedef vector<vector<p64>> vvp64;
typedef vector<p64> vp64;
typedef vector<p32> vp32;
ll MOD = 998244353;
double eps = 1e-12;
#define forn(i, e) for (ll i = 0; i < e; i++)
#define forsn(i, s, e) for (ll i = s; i < e; i++)
#define rforn(i, s) for (ll i = s; i >= 0; i--)
#define rforsn(i, s, e) for (ll i = s; i >= e; i--)
#define ln "\n"
#define dbg(x) cout << #x << " = " << x << ln
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define INF 2e18
#define fast_cin()                    \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((ll)(x).size())

int main()
{
    fast_cin();
    //ifstream cin("in.txt");
    int n;
    cin >> n;
    int k;
    cin >> k;
    vector<int> cap(k + 1);
    for (int i = 1; i <= k; i++)
    {
        cin >> cap[i];
    }
    vector<int> ask(n + 1);
    for (int i = 1; i <= n; i++)
    {
        cin >> ask[i];
    }
    vector<int> price(n + 1);
    for (int i = 1; i <= n; i++)
    {
        cin >> price[i];
    }

    vector<vector<pair<int, int>>> al(n + 1);
    for (int i = 1; i <= n; i++)
    {
        al[ask[i]].push_back({price[i], i});
    }

    for (int i = 1; i <= k; i++)
    {
        sort(al[i].rbegin(), al[i].rend());
    }

    int ans = 0;
    vector<pair<int, int>> rem;
    for (int i = 1; i <= k; i++)
    {
        while (al[i].size() > cap[i])
        {
            rem.push_back(al[i][al[i].size() - 1]);
            ans += al[i][al[i].size() - 1].first;
            al[i].pop_back();
        }
    }
    sort(rem.rbegin(), rem.rend());
    priority_queue<pair<int,int>> pq;
    for (int i = 1; i <= k; i++)
    {
        if(al[i].size() == 0){
            pq.push({INT_MAX, i});
        }
        else{
            pq.push({cap[i] - al[i].size(), i});
        }
    }

    while(rem.size()){
        int i = pq.top().second;
        al[i].push_back(rem[rem.size() - 1]);
        pq.pop();
        pq.push({cap[i] - al[i].size(), i});
        rem.pop_back();
    }

    for (int i = 1; i <= k; i++)
    {
        for (int j = al[i].size() - 1; j >= 1; j--)
        {
            rem.push_back(al[i][j]);
        }
    }
    sort(rem.rbegin(), rem.rend());

    for(int i = 1; i <= k; i++){
        if(al[i].size() == 0){
            ans += rem[rem.size() - 1].first;
            al[i].push_back(rem[rem.size() - 1]);
            rem.pop_back();
        }
    }

    vector<int> anss(n + 1);
    for(int i = 1; i <= k; i++){
        for(int j = 0; j < al[i].size(); j++){
            anss[al[i][j].second] = i;
        }
    }
    
    cout << ans << '\n';
    for(int i = 1; i <= n; i++){
        cout << anss[i] << " ";
    }

    return 0;
}
SubtaskSumTestVerdictTimeMemory
base40/50
1Accepted0/03ms1824 KiB
2Accepted0/04ms2724 KiB
3Accepted2/22ms2228 KiB
4Accepted2/22ms2312 KiB
5Accepted2/23ms2444 KiB
6Accepted2/23ms2672 KiB
7Accepted2/23ms2872 KiB
8Accepted2/23ms3088 KiB
9Accepted2/22ms3176 KiB
10Accepted2/22ms3172 KiB
11Accepted2/23ms3416 KiB
12Accepted2/23ms3540 KiB
13Partially correct1/23ms3684 KiB
14Accepted3/33ms3684 KiB
15Partially correct1/38ms5592 KiB
16Partially correct1/318ms8140 KiB
17Accepted3/326ms9492 KiB
18Partially correct1/339ms13156 KiB
19Accepted3/346ms13976 KiB
20Accepted3/357ms16176 KiB
21Accepted3/361ms16452 KiB
22Partially correct1/464ms20880 KiB