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

using namespace std;

using ll = long long;
constexpr ll impossible = 1L<<60;

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	ll n, k, m;
	cin >> n >> k >> m;
	vector<vector<pair<ll, ll>>> byColor(k);
	ll col, mass, cos;
	for(int i = 0; i < n; i++) {
		cin >> col >> mass >> cos;
		byColor[col - 1].push_back({mass, cos});
	}
	for(auto v: byColor) {
		if (v.empty()) {
			cout << "0\n";
			for(int i = 1; i < m; i++) {
				cout << "-1\n";
			}
			return 0;
		}
	}
	vector<ll> res1(m, impossible);
	vector<ll> res2(m, impossible);
	res1[0] = 0;
	auto* curr = &res1;
	auto* next = &res2;
	for(auto v: byColor) {
		for(auto& x: *next) {
			x = impossible;
		}
		for(ll st = 0; st < m; st++) {
			ll base = curr->at(st);
			if (base != impossible) {
				for(auto [mass, cos]: v) {
					ll e = (st + mass) % m;
					next->at(e) = min(next->at(e), base + cos);
				}
			}
		}
		swap(curr, next);
	}
	for(auto& x: *next) {
		x = impossible;
	}
	next->at(0) = 0;
	for(ll st = 0; st < m; st++) {
		ll base = curr->at(st);
		if (base != impossible) {
			for(ll mul = 1; mul < m; mul++) {
				ll e = (st * mul) % m; 
				next->at(e) = min(next->at(e), mul * base);
			}
		}
	}
	swap(curr, next);
	for(auto& x: *next) {
		x = impossible;
	}
	next->at(0) = 0;
	for(ll st = 0; st < m; st++) {
		ll base = curr->at(st);
		if (base != impossible) {
			for(ll sec = 0; sec < m; sec++) {
				ll e = (st + sec) % m; 
				next->at(e) = min(next->at(e), base + curr->at(sec));
			}
		}
	}
	swap(curr, next);
	for(auto r: *curr) {
		if (r == impossible) cout << "-1\n";
		else cout << r << "\n";
	}
}