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 <iostream>
#include <vector>
#include <array>
#include <cassert>
#include <cstring>

using ll = long long;

constexpr int max_n = 7e3 + 10;
constexpr int max_m = 7e3 + 10;
constexpr int max_k = 7e3 + 10;

ll cost1[max_k][max_m];

int input_k[max_n];
int input_m[max_n];
int input_c[max_n];

std::vector<int> index_of_color[max_k];

ll sat_add(ll a, ll b)
{
	if (a == -1ll || b == -1ll)
		return -1ll;
	else
		return a + b;
}
ll sat_min(ll a, ll b)
{
	if (a == -1ll)
		return b;
	if (b == -1ll)
		return a;
	return std::min(a, b);
}
void self_sat_min(ll& a, ll b)
{
	a = sat_min(std::move(a), std::move(b));
}

ll fmod(ll m1, ll m2)
{
	if (m1 >= m2)
		return m1 - m2;
	else
		return m1;
}
std::array<ll, max_n> pot1;
std::array<ll, max_n> pot2;

int main()
{
	std::ios_base::sync_with_stdio(0);
	std::cin.tie(0);

	int n, k, m;
	std::cin >> n >> k >> m;

	assert(n >= 1);
	assert(k >= 1);
	assert(m >= 1);

	for (int i = 0; i < n; ++i)
	{
		std::cin >> input_k[i] >> input_m[i] >> input_c[i];
		index_of_color[input_k[i]].push_back(i);
	}
	for (int i = 0; i <= k; ++i)
		for (int j = 0; j < m; ++j)
			cost1[i][j] = -1;
	cost1[0][0] = 0;

	for(int kolor = 1; kolor <= k; ++kolor)
		for (int ind : index_of_color[kolor])
			for (int mods = 0; mods < m; ++mods)
				self_sat_min(cost1[kolor][(mods + input_m[ind]) % m], sat_add(cost1[kolor - 1][mods], input_c[ind]));

	memcpy(pot2.data(), &cost1[k][0], sizeof(ll) * m);
	pot2[0] = 0;

    int count = 0;
	do
	{
        ++count;
		pot1 = pot2;
		for (int i = 0; i < m; ++i)
			for (int j = 0; j < m; ++j)
				self_sat_min(pot2[(i + j) % m], sat_add(pot1[i], pot1[j]));
	} while (pot1 != pot2);

	for (int i = 0; i < m; ++i)
		std::cout << pot1[i] << '\n';
}