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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <bits/stdc++.h>

bool best_value[7'004];

struct Jelly {
    long long weight, price;

    Jelly(long long weight, long long price) : weight(weight), price(price) {}

    bool operator==(const Jelly & other) const {
        return weight == other.weight && price == other.price;
    }

    Jelly & operator=(const Jelly & other) {
        weight = other.weight;
        price = other.price;
        return *this;
    }
};

struct Hash {
    inline size_t operator()(const Jelly & jelly) const {
        return static_cast<size_t>(jelly.weight ^ jelly.price);
    }
};

struct Cmp {
    bool operator()(
        const std::pair<long long, long long> & a,
        const std::pair<long long, long long> & b) const {
        return a.second < b.second;
    }
};

long long n, k, m;

std::unordered_set<Jelly, Hash> JellySet[7'004];
long long m_output[7'004][2];
bool usable[7'004][2];

long long output[7'004];
std::multiset<std::pair<long long, long long>, Cmp> pq;

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);
    std::cout.tie(NULL);

    for (int i = 0; i < 7'004; ++i) {
        for (int j = 0; j < 2; ++j) {
            m_output[i][j] = LONG_LONG_MAX;
        }
    }

    std::cin >> n >> k >> m;
    for (int i = 0; i < n; i++) {
        long long color, weight, price;
        std::cin >> color >> weight >> price;
        auto newJelly = Jelly(weight, price);
        JellySet[color].insert(newJelly);
    }

    for (int i = 1; i <= k; ++i) {
        if (JellySet[i].empty()) {
            std::cout << 0 << std::endl;
            for (int j = 1; j < m; ++j) {
                std::cout << -1 << std::endl;
            }
            return 0;
        }
    }

    for (auto & jelly : JellySet[1]) {
        if (m_output[jelly.weight % m][1] > jelly.price) {
            usable[jelly.weight % m][1] = true;
            m_output[jelly.weight % m][1] = jelly.price;
        }
    }

    for (int i = 2; i <= k; ++i) {
        for (int j = 0; j < m; ++j) {
            usable[j][i % 2] = false;
            m_output[j][i % 2] = LONG_LONG_MAX;
        }
        
        for (auto & jelly : JellySet[i]) {
            for (int j = 0; j < m; ++j) {
                if (usable[j][(i - 1) % 2]) {
                    long long newWeight = (j + jelly.weight) % m;
                    if (m_output[newWeight][i % 2] == LONG_LONG_MAX) {
                        usable[newWeight][i % 2] = true;
                        m_output[newWeight][i % 2] = m_output[j][(i - 1) % 2] + jelly.price;
                    }
                    else if (m_output[newWeight][i % 2] > m_output[j][(i - 1) % 2] + jelly.price) {
                        m_output[newWeight][i % 2] = m_output[j][(i - 1) % 2] + jelly.price;
                    }
                }
            }
        }
    }

    for (int i = 0; i < m; ++i) {
        m_output[i][0] = m_output[i][k % 2];
    }

    for (int i = 0; i < m; ++i) {
        m_output[i][1] = LONG_LONG_MAX;
    }

    for (int i = 1; i < m; ++i) {
        if (m_output[i][0] != LONG_LONG_MAX) {
            pq.insert({i, m_output[i][0]});
        }
    }

    int i = 0;
    while (!pq.empty()){
        auto [idx, value] = *pq.begin();

        pq.erase(pq.begin());
        if (best_value[idx]) {
            continue;
        }

        best_value[idx] = true;

        for (int j = 0; j < m; ++j) {
            m_output[j][(i + 1) % 2] = m_output[j][i % 2];
        }

        for (int j = 0; j < m; ++j) {
            if (m_output[j][i % 2] == LONG_LONG_MAX) {
                continue;
            }
            long long newWeight = (j + idx) % m;
            long long newValue = m_output[j][i % 2] + value;
            if (m_output[newWeight][(i + 1) % 2] > newValue) {
                m_output[newWeight][(i + 1) % 2] = newValue;
                pq.insert({newWeight, newValue});
            }
        }
        ++i;
    }

    
    
    std::cout << 0;
    for (int x = 1; x < m; ++x) {
        std::cout << '\n' << (m_output[x][i % 2] == LONG_LONG_MAX ? -1 : m_output[x][i % 2]);
    }
    std::cout << std::endl;
    
    return 0;
}