40532023-03-11 10:00:051478Óvodacpp17Partially correct 40/5065ms20380 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());
    }

    long long 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/03ms1828 KiB
2Accepted0/04ms2768 KiB
3Accepted2/22ms2256 KiB
4Accepted2/22ms2336 KiB
5Accepted2/22ms2568 KiB
6Accepted2/22ms2544 KiB
7Accepted2/23ms2676 KiB
8Accepted2/23ms2836 KiB
9Accepted2/23ms3060 KiB
10Accepted2/23ms3276 KiB
11Accepted2/22ms3364 KiB
12Accepted2/22ms3436 KiB
13Partially correct1/23ms3608 KiB
14Accepted3/33ms3884 KiB
15Partially correct1/38ms5380 KiB
16Partially correct1/317ms7612 KiB
17Accepted3/325ms9088 KiB
18Partially correct1/335ms12680 KiB
19Accepted3/343ms13500 KiB
20Accepted3/354ms15584 KiB
21Accepted3/357ms15636 KiB
22Partially correct1/465ms20380 KiB