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
#include <algorithm>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define VAR(v,w) __typeof(w) v=(w)
#define FORE(it,c) for(VAR(it,(c).begin());it!=(c).end();++it)
#define PB push_back
#define MP make_pair
#define FT first
#define SD second
#define INT(x) int x; scanf("%d", &x)

typedef long long LL;
typedef vector<int> VI;
typedef vector<VI> VVI;

const LL INF = 4000000000000000000LL;
int ko[7000], ma[7000], c[7000];
LL b[2][7000];
LL r[7000];

int main() {
	INT(n);
	INT(k);
	INT(m);
	REP(i,n) scanf("%d%d%d", &ko[i], &ma[i], &c[i]);
	REP(i,n) --ko[i];
	VVI ko2i(k);
	REP(i,n) ko2i[ko[i]].PB(i);
	REP(j,m) b[0][j] = INF;
	b[0][0] = 0;
	REP(kk,k) {
		REP(j,m) b[(kk + 1) & 1][j] = INF;
		FORE(it,ko2i[kk]) REP(j,m) {
			int jj = (j + ma[*it]) % m;
			b[(kk + 1) & 1][jj] = min(b[(kk + 1) & 1][jj], b[kk & 1][j] + c[*it]);
		}
	}
	int kk = k & 1;
	priority_queue<pair<int, LL> > q;
	REP(j,m) r[j] = INF;
	r[0] = 0;
	q.push(MP(0, 0));
	while (!q.empty()) {
		int v = q.top().FT;
		LL cc = q.top().SD;
		q.pop();
		if (cc > r[v]) continue;
		REP(j,m) {
			int w = (v + j) % m;
			LL rr = cc + b[kk][j];
			if (rr < r[w]) {
				r[w] = rr;
				q.push(MP(w, rr));
			}
		}
	}
	REP(j,m) printf("%lld\n", r[j] < INF ? r[j] : -1);
}