1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
#include <iostream>
#include <vector>

using namespace std;

class JellyBean {
public:
    int weight;
    long long price;
};

int n, k, m;
vector<vector<JellyBean>> colours;
vector<long long> *prices = nullptr;

vector<long long> *createPrices() {
    return new vector<long long>(m, -1);
}

void updatePrices(vector<long long> *newPrices) {
    delete prices;
    prices = newPrices;
}

bool computeSingleSets() {
    for (int i = 0; i < k; i++) {
        if (colours[i].size() == 0) {
            return false;
        }
        vector<long long> *newPrices = createPrices();
        for (int r = 0; r < m; r++) {
            if (prices->at(r) > 0 || i + r == 0) {
                for (auto &jellyBean : colours[i]) {
                    int weight = (r + jellyBean.weight) % m;
                    long long price = max(0LL, prices->at(r)) + jellyBean.price;
                    if (newPrices->at(weight) < 0 || newPrices->at(weight) > price) {
                        newPrices->at(weight) = price;
                    }
                }
            }
        }
        updatePrices(newPrices);
    }
    return true;
}

bool newPrice(int weight, long long price) {
    if (prices->at(weight) < 0 || prices->at(weight) > price) {
        prices->at(weight) = price;
        return true;
    }
    return false;
}

void computeCompositions() {
    bool changed = true;
    while (changed) {
        changed = false;
        for (int r1 = 0; r1 < m; r1++) {
            for (int r2 = 0; r2 <= r1; r2++) {
                long long price1 = prices->at(r1);
                long long price2 = prices->at(r2);
                if (price1 > 0 && price2 > 0) {
                    if (newPrice((r1 + r2) % m, price1 + price2)) {
                        changed = true;
                    }
                }
            }
        }
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> n >> k >> m;
    colours.resize(k);
    prices = createPrices();

    for (int i = 0; i < n; i++) {
        int colour;
        cin >> colour;
        JellyBean jellyBean;
        cin >> jellyBean.weight;
        cin >> jellyBean.price;
        colours[colour - 1].push_back(jellyBean);
    }

    if (!computeSingleSets()) {
        cout << 0 << endl;
        for (int r = 1; r < m; r++) {
            cout << -1 << endl;
        }
        return 0;
    }

    computeCompositions();
    prices->at(0) = 0;
    for (int r = 0; r < m; r++) {
        cout << prices->at(r) << endl;
    }
    return 0;
}