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
#include <bits/stdc++.h>

using namespace std;

const int maxn = 1e5 + 10;
const long long INF = 1e18;
int color[maxn], weight[maxn], cost[maxn];
vector<int> ids[maxn];

void upd(long long& a, long long b) {
    a = min(a, b);
}

int main() {
    // freopen("input.txt", "r", stdin);
    ios_base::sync_with_stdio(false);
    int n, k, m;
    cin >> n >> k >> m;
    for (int i = 1; i <= n; ++i) {
        cin >> color[i] >> weight[i] >> cost[i];
        ids[color[i]].push_back(i);
    }
    vector<long long> dp0(m), dp1(m);
    for (int i = 0; i < m; ++i) {
        dp0[i] = INF;
    }
    dp0[0] = 0;
    for (int c = 1; c <= k; ++c) {
        for (int j = 0; j < m; ++j) dp1[j] = INF;
        for (int j = m - 1; j >= 0; --j) {
            if (dp0[j] == INF) continue;
            for (const auto& id : ids[c]) {
                int nw = j + weight[id];
                nw %= m;
                upd(dp1[nw], dp0[j] + cost[id]);
            }
        }
        for (int j = 0; j < m; ++j) dp0[j] = dp1[j];
    }
    dp0[0] = 0;

    vector<pair<int, long long> > pairs;
    for (int i = 0; i < m; ++i) {
        if (dp0[i] != INF) pairs.push_back({i, dp0[i]});
    }

    vector<long long> ans(m, INF);

    vector<bool> used(m, true);
    for (int i = 0; i < m; ++i) if (dp0[i] != INF) ans[i] = dp0[i];

    for (int i = 0; i < m; ++i) {
        bool ok = false;
        int id = -1;
        long long x = INF;
        for (int j = 0; j < m; ++j) {
            if (!used[j]) continue;
            if (id == -1 || ans[j] < x) {
                x = ans[j];
                id = j;
                ok = true;
            }
        }
        if (!ok) break;
        used[id] = false;
        for (const auto& p : pairs) {
            int nw = id + p.first;
            nw %= m;
            upd(ans[nw], ans[id] + p.second);
        }
    }

    for (int i = 0; i < m; ++i) {
        if (ans[i] == INF) ans[i] = -1;
        cout << ans[i] << endl;
    }
    return 0;
}