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
#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
const long long INF=1e18;
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int n, k, m; cin>>n>>k>>m;
	vector<vector<pair<int, int>>>zel(k+1);
	for (int i=0; i<n; i++)
	{
		int col, waga, cen; cin>>col>>waga>>cen;
		zel[col].push_back({waga, cen});
	}
	vector<long long>dp(m, INF); dp[0]=0;
	for (int i=1; i<=k; i++)
	{
		vector<long long>pom(m, INF);
		for (auto j : zel[i])
		{
			for (int l=0; l<m; l++)
			{
				pom[l]=min(pom[l], dp[l-j.f+(l-j.f<0)*m]+j.s);
			}
		}
		swap(dp, pom);
	}
	//~ for (int i=0; i<m; i++) cout<<i<<" "<<dp[i]<<endl;
	//~ return 0;
	vector<long long>ans(m, INF); ans[0]=0;
	for (int i=1; i<m; i++)
	{
		for (int j=__gcd(i, m); j>=1; j--)
		{
			int l=j, pre=(m+j-i)%m;
			do
			{
				ans[l]=min(ans[l], ans[pre]+dp[i]);
				pre=l;
				l+=i; l-=(l>=m)*m;
			}while (l != j);
			l=j, pre=(m+j-i)%m;
			do
			{
				ans[l]=min(ans[l], ans[pre]+dp[i]);
				pre=l;
				l+=i; l-=(l>=m)*m;
			}while (l != j);
		}
	}
	for (auto i : ans) cout<<(i==INF? -1 : i)<<"\n";
}