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
#include <iostream>
#include <tuple>
#include <vector>
#include <utility>
#include <unordered_map>
#include <algorithm>

int n, k, m;
std::vector< std::tuple<int, int, int> > v;

void input() {
    std::cin >> n >> k >> m;
    int x, y, z;
    for (int i = 0; i < n; i++) {
        std::cin >> x >> y >> z;
        v.push_back(std::make_tuple(x, y, z));
    }
}

std::unordered_map< int, std::vector< std::pair<int, int> > > groups;

void group_by_color() {
    for (auto z : v) {
        auto [kk, mm, c] = z;
        if (!groups.contains(kk)) {
            groups[kk] = std::vector< std::pair<int, int> >();
        }
        groups[kk].push_back(std::make_pair(mm, c));
    }
}

void search(std::vector<long long> & result, std::unordered_map<int, long long> & prev) {
    std::unordered_map<int, long long> new_map(prev);
    for (int color = 1; color <= k; color++) {
        std::unordered_map<int, long long> next_map;
        for (auto mc : groups[color]) {
            for (auto e : new_map) {
                int new_mass = (mc.first + e.first) % m;
                long long new_cost = mc.second + e.second;
                if (next_map.contains(new_mass)) {
                    next_map[new_mass] = std::min(next_map[new_mass], new_cost);
                } else {
                    next_map[new_mass] = new_cost;
                }
            }
        }
        new_map = next_map;
    }

    for (auto e : new_map) {
        if (result[e.first] == -1 || result[e.first] > e.second) {
            result[e.first] = e.second;
        }
    }
    prev = new_map;
}

void solve() {
    group_by_color();

    if (groups.size() != k) {
        std::cout << "0\n";
        for (int i = 1; i < m; i++) {
            std::cout << "-1\n";
        }
        return;
    }

    std::vector<long long> result(m);
    result[0] = 0;
    for (int i = 1; i < m; i++) {
        result[i] = -1;
    }

    std::unordered_map<int, long long> prev;
    prev[0] = 0;
    for (int i = 1; i < m; i++) {
        search(result, prev);
    }

    for (auto r : result) {
        std::cout << r << "\n";
    }
}

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

    input();
    solve();

    return 0;
}