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

using namespace std;
typedef long long ll;
const int MAXN=7000;
const ll INF=1e17;

vector<pair<int, ll>> zelki[MAXN+5];
ll dp1[2][MAXN+5];
ll dp2[MAXN+5];

void solve()
{
	int n,k,m;
	cin >> n >> k >> m;
	for(int i=0;i<n;i++){
		int ki,mi;
		ll ci;
		cin >> ki >> mi >> ci;
		zelki[ki].push_back({mi, ci});
	}
	
	for(int i=1;i<=k;i++){
		if(zelki[i].size()==0){
			cout << 0 << "\n";
			for(int i=1;i<m;i++){
				cout << -1 << "\n";
			}
			return;
		}
	}
	
	for(int i=1;i<m;i++){
		dp1[0][i]=INF;
	}
	int l=0;
	for(int i=1;i<=k;i++){
		for(int j=0;j<m;j++){
			dp1[l^1][j]=INF;
		}
		for(pair<int, ll> pr : zelki[i]){
			int mi = pr.first;
			ll ci = pr.second;
			for(int j=0;j<m;j++){
				dp1[l^1][(j+mi)%m] = min(dp1[l^1][(j+mi)%m], dp1[l][j] + ci);
			}
		}
		l^=1;
	}
	/*for(int i=0;i<m;i++){
		cout << dp1[l][i] << " ";
	}*/
	vector<pair<int, ll>> W;
	for(int i=0;i<m;i++){
		if(dp1[l][i]!=INF){
			W.push_back({i, dp1[l][i]});
		}
	}
	for(int i=1;i<m;i++){
		dp2[i]=INF;
	}
	priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> Q;
	Q.push({0, 0});
	dp2[0]=0;
	while(!Q.empty()){
		int v = Q.top().second;
		ll dv = Q.top().first;
		Q.pop();
		if(dv != dp2[v]) continue;
		for(int i=0;i<W.size();i++){
			if(dp2[(v + W[i].first) % m] > dp2[v] + W[i].second){
				dp2[(v + W[i].first) % m] = dp2[v] + W[i].second;
				Q.push({dp2[(v + W[i].first) % m], (v + W[i].first) % m});
			}
		}
	}
	for(int i=0;i<m;i++){
		if(dp2[i]!=INF) cout << dp2[i] << "\n";
		else cout << -1 << "\n";
	}
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	solve();
	
	return 0;
}