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
#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef long long ll;

const ll inf = 1e18+7;
const int maxn = 7e3+7;

int N, K, M;
map<pair<int,int>, ll> mp;
vector<pair<int,ll>> zelki[maxn];

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    
    //dane
    cin >> N >> K >> M;
    for (int i=0; i<N; i++){
		ll k, m, c; cin >> k >> m >> c;
		if (mp[{k, m}] == 0) mp[{k, m}] = c;
		else if (mp[{k, m}] > c) mp[{k, m}] = c;
	}
	for (auto ele : mp){
		zelki[ele.fi.fi].push_back({ele.fi.se, ele.se});
	}
	
	//plecak - wygenerowanie przedmiotow, tak aby kazdy zawieral po jednym z kazdego koloru
	vector<ll> dp(M, inf);
	for (int k=1; k<=K; k++){
		vector<ll> ndp(M, inf);
		if (k == 1) dp[0] = 0;
		for (auto [m, c] : zelki[k]){
			for (int i=0; i<M; i++){
				if (dp[i] != inf){
					ndp[(i+m) % M] = min(ndp[(i+m) % M], dp[i] + c);
				}
			}
		}
		dp = ndp;
	}
	
	//wykonanie plecaka n razy na utworzonych przedmiotach
	dp[0] = 0;
	while (true){
		vector<ll> ndp(M, inf), last_dp = dp;
		for (int i=0; i<M; i++){
			if (dp[i] == inf) continue;
			for (int j=0; j<M; j++){
				if (dp[j] == inf) continue;
				ndp[(i+j) % M] = min(ndp[(i+j) % M], dp[i] + dp[j]);
			}
		}
		for (int i=0; i<M; i++){
			dp[i] = min(dp[i], ndp[i]);
		}
		
		//przerwij kiedy nic sie nie zmienia
		if (last_dp == dp) break;
	}
	
	for (int i=0; i<M; i++){
		if (dp[i] == inf) cout << -1 << '\n';
		else cout << dp[i] << '\n';
	}
	
    return 0;
}
/*

3 2 6
1 2 1
2 2 2
1 1 5

2 3 3
1 1 1
3 1 1

*/