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
#include <iostream>
#include <limits>
#include <unordered_map>
#include <vector>
using namespace std;

struct Jellybean {
    int weight{}, price{};
};

constexpr long long infinity = numeric_limits<long long>::max();

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, k, m;
    cin >> n >> k >> m;
    unordered_map<int, vector<Jellybean>> beansPerColor;
    for (int i = 0; i < n; ++i) {
        int ki, mi, ci;
        cin >> ki >> mi >> ci;
        beansPerColor[ki].push_back({mi, ci});
    }
    if (size(beansPerColor) < k) {
        cout << 0 << '\n';
        for (int i = 1; i < m; ++i) {
            cout << -1 << '\n';
        }
        return 0;
    }
    vector<long long> cheapestRows;
    cheapestRows.resize(m, infinity);
    cheapestRows[0] = 0;
    for (const auto &sameColorBeans : beansPerColor) {
        vector<long long> cheapestRowsNew;
        cheapestRowsNew.resize(m, infinity);
        for (int i = 0; i < m; ++i) {
            if (cheapestRows[i] != infinity) {
                for (auto bean : sameColorBeans.second) {
                    if (cheapestRowsNew[(i + bean.weight) % m] > cheapestRows[i] + bean.price) {
                        cheapestRowsNew[(i + bean.weight) % m] = cheapestRows[i] + bean.price;
                    }
                }
            }
        }
        cheapestRows = std::move(cheapestRowsNew);
    }
    for (;;) {
        bool finish = true;
        for (int i = 0; i < m; ++i) {
            for (int j = i; j < m; ++j) {
                if (cheapestRows[i] != infinity and cheapestRows[j] != infinity) {
                    if (cheapestRows[(i + j) % m] > cheapestRows[i] + cheapestRows[j]) {
                        cheapestRows[(i + j) % m] = cheapestRows[i] + cheapestRows[j];
                        finish = false;
                    }
                }
            }
        }
        if (finish) {
            break;
        }
    }
    cheapestRows[0] = 0;
    for (int i = 0; i < m; ++i) {
        cout << (cheapestRows[i] == infinity ? -1 : cheapestRows[i]) << '\n';
    }
}