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
#include <cstdio>
#include <utility>
#include <queue>
#include <vector>

#define REP(a, n) for (int a = 0; a < (n); ++a)
#define FOR(a, b, c) for (int a = (b); a <= (c); ++a)

typedef long long LL;

using namespace std;

template<class T> inline int SIZE(const T &t) { return t.size(); }

#define INF 1'000'000'000'000'000'001

//////////////////////

int N, K, M;
vector<pair<int,int>> zel[7005];
LL wyn[7005][7005], wyn2[7005];


priority_queue<pair<LL, int>> Q;

inline void enQ(int m, LL c) {
    if (c<wyn2[m]) {
        wyn2[m] = c;
        Q.emplace(-c, m);
    }
}

int main() {
    scanf("%d%d%d", &N, &K, &M);
    REP(a, N) {
        int k, m, c;
        scanf("%d%d%d", &k, &m, &c);
        zel[k-1].emplace_back(m, c);
    }
    REP(k, K+1) REP(m, M) wyn[k][m] = INF;
    REP(m, M) wyn2[m] = INF;
    wyn[0][0] = 0;
    REP(k, K)
        REP(m, M)
            //if (wyn[k][m]<INF)
                for (auto p : zel[k])
                    wyn[k+1][(m+p.first)%M] = min(wyn[k+1][(m+p.first)%M], wyn[k][m]+p.second);
    enQ(0, 0);
    while(!Q.empty()) {
        LL c = -Q.top().first;
        int m = Q.top().second;
        Q.pop();
        if (wyn2[m]!=c)
            continue;
        REP(m2, M)
            enQ((m+m2)%M, c+wyn[K][m2]);
    }
    REP(m, M)
        printf("%lld\n", wyn2[m]==INF ? -1 : wyn2[m]);
}