CREDSCORE codechef solution

#include<bits/stdc++.h>
using namespace std;

#define int long long

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int t;
    cin >> t;

    while (t--) {
        int n, m, x;
        cin >> n >> m >> x;

        vector<pair<int, int>> a(n);
        for (int i = 0; i < n; i++) {
            cin >> a[i].first;
            a[i].second = i + 1;
        }

        sort(a.begin(), a.end());

        multiset<pair<int, int>> s;
        for (int i = 0; i < m; i++) {
            s.insert({0, i + 1});
        }

        vector<int> ans(n);
        for (int i = 0; i < n; i++) {
            auto it = s.begin();
            pair<int, int> cur = *it;
            s.erase(it);
            cur.first += a[i].first;
            ans[a[i].second - 1] = cur.second;
            s.insert(cur);
        }

        int max_time = 0, min_time = LLONG_MAX;
        for (int i = 0; i < m; i++) {
            auto it = s.find({0, i + 1});
            if (it != s.end()) {
                s.erase(it);
            }
        }

        for (auto p : s) {
            max_time = max(max_time, p.first);
            min_time = min(min_time, p.first);
        }

        if (max_time - min_time > x) {
            cout << "NO\n";
        } else {
            cout << "YES\n";
            for (int i = 0; i < n; i++) {
                cout << ans[i] << " ";
            }
            cout << "\n";
        }
    }

    return 0;
}