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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <bits/stdc++.h>
using namespace std;

const char *ENDL = "\n";
map<int, map<int, long long int>> colorMap;
//struct ColorTable {
//	const int MOD;
//	ColorTable(const int mod) :
//			MOD(mod) {
//	}
//};

void printNotExists(int m) {
	cout << 0 << ENDL;
	for (int i = 1; i < m; i++) {
		cout << "-1" << ENDL;
	}
}

void printMap() {
	for (auto entry : colorMap) {
		cout << entry.first << ":" << endl;
		for (auto pair : entry.second) {
			cout << "\t" << pair.first << " " << pair.second << endl;
		}
	}
}

struct ColorTableBuilder {
	const int MOD;
	ColorTableBuilder(const int mod) :
			MOD(mod) {
	}
	void joinColorTables(vector<vector<long long int>> &all,
			vector<vector<long long int>> &one) {
		long long int allRowValue, currentResultValue, newValue;
		int resultIndex;
		for (int col = 1; col < MOD; col++) {
			vector<long long int> resultCol(MOD, -1);
			vector<int> notEmptyRestOneIndexes;
			for (int i = 0; i < MOD; i++) {
				if (one[i][col] >= 0) {
					notEmptyRestOneIndexes.push_back(i);
				}
			}
			for (int rowi = 0; rowi < MOD; rowi++) {
				allRowValue = all[rowi][col];
				if (allRowValue >= 0) {
					for (int oneIndex : notEmptyRestOneIndexes) {
						resultIndex = (oneIndex + rowi) % MOD;

//						currentResultValue = resultCol[rowi];
						currentResultValue = resultCol[resultIndex];
						newValue = all[rowi][col] + one[oneIndex][col];
						if (currentResultValue < 0) {
							resultCol[resultIndex] = newValue;
						} else if (currentResultValue > newValue) {
							resultCol[resultIndex] = newValue;
						}
					}
				}
			}
			for (int i = 0; i < MOD; i++) {
				all[i][col] = resultCol[i];
			}
		}
	}

	void fillColorTable(vector<vector<long long int>> &ctb,
			const map<int, long long int> &colorData) {
		ctb[0][0] = 0;
		int updatedRow;
		long long int currentValue, newValue, prevColValue;
		for (int col = 1; col < MOD; col++) {
			// clear col
			for (int row = 0; row < MOD; row++) {
				ctb[row][col] = -1;
			}

			for (int row = 0; row < MOD; row++) {
				prevColValue = ctb[row][col - 1];
				if (prevColValue >= 0) {
					for (auto colorEntry : colorData) {
						updatedRow = (row + colorEntry.first) % MOD;
						currentValue = ctb[updatedRow][col];
						newValue = prevColValue + colorEntry.second;
						if (currentValue < 0) {
							ctb[updatedRow][col] = newValue;
						} else if (currentValue > newValue) {
							ctb[updatedRow][col] = newValue;
						}
					}
				}
			}
		}
	}

	void print2DVector(vector<vector<long long int>> &v) {
		for (int i = 0; i < MOD; i++) {
			for (int j = 0; j < MOD; j++)
				cout << v[i][j] << "\t ";
			cout << endl;
		}
		cout << "---" << endl;
	}
};

int main() {
//	ifstream cin("tests/0a.in");
//	ifstream cin("tests/4.in");

	cin.tie(NULL);
	cout.tie(NULL);
	ios_base::sync_with_stdio(false);

	int n, k, m, ki, mi, miCounter = 0;
	long long int ci, tmp;
	cin >> n;
	cin >> k;
	cin >> m;
	for (int i = 0; i < n; i++) {
		cin >> ki;
		cin >> mi;
		cin >> ci;

		if (mi != m) {
			miCounter++;
		}

		tmp = colorMap[ki][mi];
		if (tmp == 0) {
			colorMap[ki][mi] = ci;
		} else {
			colorMap[ki][mi] = min(tmp, ci);
		}
	}
	if (colorMap.size() != (unsigned) k || miCounter == 0) {
		printNotExists(m);
		return 0;
	}

//	printMap();

	ColorTableBuilder ctb(m);

	// Initializing the 2-D vector
	vector<vector<long long int>> allColorsTable(m,
			vector<long long int>(m, -1));
	vector<vector<long long int>> oneColorTable(m,
			vector<long long int>(m, -1));

	ctb.fillColorTable(allColorsTable, colorMap[1]);
//	ctb.print2DVector(allColorsTable);
	for (int i = 2; i <= k; i++) {
		ctb.fillColorTable(oneColorTable, colorMap[i]);
		ctb.joinColorTables(allColorsTable, oneColorTable);
	}

//	ctb.print2DVector(oneColorTable);
//	ctb.print2DVector(allColorsTable);
	long long int out;
	int j = 0;
	for (int i = 0; i < m; i++) {
		out = -1;
		for (j = 0; j < m; j++) {
			if (allColorsTable[i][j] >= 0) {
				if (out < 0) {
					out = allColorsTable[i][j]; // << ENDL;
				} else if (allColorsTable[i][j] < out) {
					out = allColorsTable[i][j]; // << ENDL;
				}
			}
		}
		cout << out << ENDL;
	}
	return 0;
}