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
//泥の分際で私だけの大切を奪おうだなん
#include<bits/stdc++.h>
using namespace std;
#define ll long long
inline int read(){
   int s=0,w=1;
   char ch=getchar();
   while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
   while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
   return s*w;
}
ll f[7003],g[7003];
bool vis[7003];
vector<pair<int,int>> a[7003];
signed main()
{
	int n=read(),m=read(),p=read();
	for(int i=1,x,y,z; i<=n; ++i)
		x=read(),y=read()%p,z=read(),
		a[x].push_back({y,z});
	memset(f,0x3f,sizeof(f)),f[0]=0;
	for(int i=1; i<=m; ++i)
	{
		swap(f,g),memset(f,0x3f,sizeof(f));
		for(int j=0; j<p; ++j)
			for(auto [x,y]:a[i])
				if(j+x<p) f[j+x]=min(f[j+x],g[j]+y);
				else f[j+x-p]=min(f[j+x-p],g[j]+y);
	}
	memset(g,0x3f,sizeof(g)),g[0]=0;
	for(int r=0,x; r<p; ++r)
	{
		x=p;
		for(int i=0; i<p; ++i)
			if(!vis[i]&&g[i]<=g[x]) x=i;
		vis[x]=1;
		for(int i=0; i<p; ++i)
			if(x+i<p) g[x+i]=min(g[x+i],g[x]+f[i]);
			else g[x+i-p]=min(g[x+i-p],g[x]+f[i]);
	}
	for(int i=0; i<p; ++i) 
		if(g[i]>1e18) puts("-1");
		else printf("%lld\n",g[i]);
	return 0;
}