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

long long dp1[2][7010];
long long odl[7010];
bool odw[7010];
pair<long long,int>kra[7010];
vector<pair<int,int>>zelki[7010];
int it[7010];
int main()
{
	int n, m, k, i, j, a, b, c;
	scanf("%d%d%d", &n, &k, &m);
	for(i=0;i<n;i++){
		scanf("%d%d%d", &a, &b, &c);
		a--;
		zelki[a].push_back({b,c});
	}
	for(i=0;i<m;i++)dp1[0][i] = 1000000000000000000;
	dp1[0][0] = 0;
	for(i=0;i<k;i++){
		for(j=0;j<m;j++)
			dp1[(i+1)%2][j] = 1000000000000000000;
		for(j=0;j<m;j++)
			for(auto ij: zelki[i])
				dp1[(i+1)%2][(j+ij.first)%m] = min(dp1[(i+1)%2][(j+ij.first)%m], dp1[i%2][j]+ij.second);
	}
	for(i=0;i<m;i++)
		kra[i] = {dp1[k%2][i], i};
	sort(kra, kra+m);
	odw[0] = 1;
	for(i=0;i<m;i++){
		pair<long long, int>best = {1000000000000000000, 0};
		for(j=0;j<m;j++)
			if(odw[j]==1){
				while(it[j]<m && kra[it[j]].first<1000000000000000000 && odw[(j+kra[it[j]].second)%m]==1)
					it[j]++;
				if(it[j]<m && kra[it[j]].first<1000000000000000000)
					best = min(best, make_pair(odl[j]+kra[it[j]].first, (j+kra[it[j]].second)%m));
			}
		if(best.first==1000000000000000000)
			break;
		odw[best.second] = 1;
		odl[best.second] = best.first;
	}
	for(i=0;i<m;i++){
		if(odw[i])
			printf("%lld\n", odl[i]);
		else
			printf("-1\n");
	}
}