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

using namespace std;

void brak_koloru(int m) {
	cout << "0\n";
	while (--m)
		cout << "-1\n";
	}

typedef long long ll;
int main (){
	ios::sync_with_stdio(0);
	cin.tie(0);
	int n, k, m, waga;
	
	cin >> n >> k >> m;
	vector <vector <pair <int, int>>> kolor (k+1);
	vector <ll> ceny(m);
	ll suma_c{}, suma2{};
	if (n < k) {
		brak_koloru(m);
		return 0;
		}
	//spr czy sa wszystkie kolory	
	//else {
		int ki, mi, ci, suma_m{};
		while (n--) {
			cin >> ki >> mi >> ci;
			kolor[ki].push_back({ci, mi});
		}
		for (int i{1}; i <= k; ++i)
			if (kolor[i].empty()) {
				brak_koloru(m);
				return 0;
			}
			else {
			sort(kolor[i].begin(), kolor[i].end());
			suma_c += kolor[i][0].first;
			suma_m += kolor[i][0].second;
		}
		suma_m %= m;
		if (suma_m and (ceny[suma_m] == 0 or ceny[suma_m] > suma_c)) ceny[suma_m] = suma_c;  
		
		for (int i{1}; i <= k; ++i) {
			for (int j{1}; j < kolor[i].size(); ++j) {
				suma_c += kolor[i][j].first - kolor[i][j-1].first;
				suma_m += kolor[i][j].second - kolor[i][j-1].second;
				suma_m %= m;
				if (suma_m and (ceny[suma_m] == 0 or ceny[suma_m] > suma_c)) ceny[suma_m] = suma_c;  
		}
		suma_c += kolor[i][0].first - kolor[i][kolor[i].size()-1].first;
		suma_m += kolor[i][0].second - kolor[i][kolor[i].size()-1].second;
		}
				
		
		/*
		n = k;
		for (int i{1}; k; ++i) {
			int tmp = suma_m * i % n;
			if (tmp and ceny[tmp] == 0) 
				ceny[tmp] = suma1*i,
				--k;
			//else if (suma1*i < ceny[tmp]) .....
		}
			*/	
	cout << "0\n";
	for (int i{1}; i < ceny.size(); ++i)
		if (ceny[i]) cout << ceny[i] << '\n';
		else cout << "-1\n";


	return 0;
}