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
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <bits/stdc++.h>

using namespace std;
vector <pair<int, long long> > K[8000];
long long* jedenZelekKazdegoKoloru;
long long* helperArr;
long long* swapHelp;
long long* results;
int visited[8000];

void printResultWhenNoZelekCanBeBought(const int &m){
	cout << 0 << '\n';
	for(int i = 1; i < m; ++i){
		cout << -1 << '\n';
	}
}

int main(){
	std::ios_base::sync_with_stdio(false);
	std::cout.tie(nullptr);
	std::cin.tie(nullptr);
	
	helperArr = new long long[8000];
	jedenZelekKazdegoKoloru = new long long[8000];
	results = new long long[8000];
	
	int n, k, m, a, b, start, miniId, hid, helperVisitedId;
	long long c, mini;
	
	cin >> n >> k >> m;
	
	for(int i = 0; i < n; ++i){
		cin >> a >> b >> c;
		K[a].push_back({b,c});
	}
	
	jedenZelekKazdegoKoloru[0] = 0;
	results[0] = 0;
	visited[0] = -1;
	for(int i = 1; i < m; ++i){
		results[i] = -1;
		jedenZelekKazdegoKoloru[i] = -1;
		visited[i] = -1;
	}
	
	for(int i = 1; i <= k; ++i){
		if(K[i].size() == 0){
			printResultWhenNoZelekCanBeBought(m);
			return 0;
		}
		
		for(int j = 0; j < m; ++j){
			helperArr[j] = -1;
		}
		
		for(int j = 0; j < K[i].size(); ++j){
			for(int q = 0; q < m; ++q){
				if(jedenZelekKazdegoKoloru[q] != -1){
					hid = (q + K[i][j].first) % m;
					if(helperArr[hid] != -1){
						helperArr[hid] = min(helperArr[hid], jedenZelekKazdegoKoloru[q] + K[i][j].second);
					}
					else{
						helperArr[hid] = jedenZelekKazdegoKoloru[q] + K[i][j].second;
					}
				}
			}
		}
		swapHelp = helperArr;
		helperArr = jedenZelekKazdegoKoloru;
		jedenZelekKazdegoKoloru = swapHelp;
	
	}
	results[0] = 0;

	for(int i = 0; i < m; ++i){
		if(jedenZelekKazdegoKoloru[i] != -1){
			for(int j = 0; j < m; ++j){
				if(visited[j] != i){
					visited[j] = i;
					if(results[j] != -1){
						start = j;
						hid = (j + i) % m;
						if(results[hid] != -1){
							results[hid] = min(results[hid], results[j] + jedenZelekKazdegoKoloru[i]);
						}
						else{
							results[hid] = results[j] + jedenZelekKazdegoKoloru[i];
						}
						for(int q = (j + i) % m; q != start; q = (q + i) % m){
							visited[q] = i;
							hid = (q + i) % m;
							if(results[hid] != -1){
								results[hid] = min(results[hid], results[q] + jedenZelekKazdegoKoloru[i]);
							}
							else{
								results[hid] = results[q] + jedenZelekKazdegoKoloru[i];
							}
						}
						
						hid = (j + i) % m;
						if(results[hid] != -1){
							results[hid] = min(results[hid], results[j] + jedenZelekKazdegoKoloru[i]);
						}
						else{
							results[hid] = results[j] + jedenZelekKazdegoKoloru[i];
						}
						for(int q = (j + i) % m; q != start; q = (q + i) % m){
							hid = (q + i) % m;
							if(results[hid] != -1){
								results[hid] = min(results[hid], results[q] + jedenZelekKazdegoKoloru[i]);
							}
							else{
								results[hid] = results[q] + jedenZelekKazdegoKoloru[i];
							}
						}
					}
				}
			}
		}
	}
	
	for(int i = 0; i < m; ++i){
		cout << results[i] << '\n';
	}
	return 0;
}