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
#include <bits/stdc++.h>
using namespace std;
 
#define fwd(i, a, n) for (int i = (a); i < (n); i ++)
#define rep(i, n) fwd(i, 0, n)
#define all(X) begin(X), end(X)
#define sz(X) ((int)X.size())
#define st first
#define nd second
#define pii pair<int, int>
#define vi vector<int>
#define ll long long
 
#ifdef LOC
auto &operator<<(auto &out, pair<auto, auto> a) {
	return out << "(" << a.st << ", " << a.nd << ")";
}
 
auto &operator<<(auto &out, auto a) {
	out << "{";
	for (auto b : a)
		out << b << ", ";
	return out << "}";
}
 
void dump(auto... x) { ((cerr << x << ", "), ...) << '\n'; }
#define debug(x...) cerr << __LINE__ << ": [" #x "]: ", dump(x)
#else
#define debug(...) 0
#endif

const long long INF = 1e18;
 
int32_t main() {
	ios_base::sync_with_stdio(0), cin.tie(0);
    int n, k, m; cin>>n>>k>>m;
    vector<vector<pair<int, int>>> jelly(k);
    rep(i, n){
        int a, b, c; cin>>a>>b>>c;
        b %= m;
        a--;
        jelly[a].push_back({b, c});
    }
    vector<long long> dp(2*m, INF);
    dp[0] = 0;
    rep(i, k){
        for(int j = 2*m-1; j >= 0; j--){
            long long x = INF;
            for(auto p : jelly[i]){
                if(j - p.first >= 0){
                    x = min(x, dp[j-p.first] + p.second);
                }
            }
            dp[j] = x;
        }
        for(int j = m; j < 2*m; j++){
            dp[j-m] = min(dp[j], dp[j-m]);
            dp[j] = INF;
        }
    }
    //debug(m);
    //debug(dp);
    dp[0] = INF;
    vector<pair<int, long long>> tab;
    rep(j, m){
        if(dp[j] < INF)tab.push_back({j, dp[j]});
    }
    int cnt = sz(tab);
    vector<long long> dist(m, INF);
    dist[0] = 0;
    priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> q;
    q.push({0, 0});
    while(!q.empty()){
        int v = q.top().nd; long long d= q.top().st; q.pop();
        if(dist[v] == d){
            rep(ind, cnt){
                int u = (v + tab[ind].st)%m;
                if(d + tab[ind].nd < dist[u]){
                    dist[u] = d + tab[ind].nd;
                    q.push({dist[u], u});
                }
            }
        }
    }
    rep(i, m){
        if(dist[i] < INF){
            cout<<dist[i]<<"\n";
        }else{
            cout<<"-1\n";
        }
    }
    return 0;
}