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;
constexpr int MAXN = 7e3 + 7;
constexpr ll oo = 2e18;
ll backpack[MAXN][MAXN];
ll dp[MAXN];
vector<pair<int, int>> color[MAXN];
bool visited[MAXN];
queue<int> Q;
int n, m, k;

void compute(){
	for(int i = 0; i <= m; i++){
		for(int j = 0; j <= k; j++)
			backpack[i][j] = oo;
		dp[i] = oo;
	}
	dp[0] = 0;
	backpack[0][0] = 0;
}

void add(int p, int mi, ll ci){
	for(int i = 0; i < m; i++)
		backpack[i][p] = min(backpack[i][p], backpack[(i-mi+m)%m][p-1] + ci);
}

void add2(int v, ll val){
	for(int i = 0; i < m; i++){
		dp[(i+v)%m] = min(dp[(i+v)%m], dp[i] + val);
		
		if(dp[i] < oo && !visited[i]){
			visited[i] = 1;
			Q.push(i);
		}
	}
}

void solve1(){
	for(int i = 1; i <= k; i++){
		for(auto [mi, ci] : color[i])
			add(i, mi, ci);
	}
}

void solve2(){
	for(int i = 0; i < m; i++){
		if(backpack[i][k] >= oo) continue;
		for(int j = 1; j <= m; j++){
			int reszta = (i*j)%m;
			dp[reszta] = min(dp[reszta], backpack[i][k]*(ll)j);
		}
	}
	for(int i = 0; i < m; i++){
		if(dp[i] >= oo) continue;
		visited[i] = 1;
		Q.push(i);
	}
}

void solve3(){
	while(Q.size()){
		int v = Q.front(); Q.pop();
		ll val = dp[v];
		add2(v, val);
		val = dp[v];
		add2(v, val);
	}
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cin >> n >> k >> m;
	compute();
	for(int i = 1; i <= n; i++){
		int ki, mi, ci;
		cin >> ki >> mi >> ci;
		color[ki].push_back({mi, ci});
	}
	
	solve1();
	solve2();
	solve3();
	
	for(int i = 0; i < m; i++){
		if(dp[i] >= oo) cout << "-1 \n";
		else cout << dp[i] << "\n";
	}
}