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
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>

#define P(a,n) for(int j=a;j<n;j++)
#define P3(a,n,z) for(int z=a;z<n;z++)
#define PD(pocz,kon,z) for(int z=pocz;z>=kon;z--)
#define W while

#define PB push_back
#define F first
#define S second
#define ll long long

#define O cout<<
#define I cin>>
#define endl '\n'
#define E '\n'

using namespace std;

constexpr int MN=7'007;
constexpr ll inf=1'000'000'000'000'000'000;
ll koszt[MN];
ll nkoszt[MN];
ll opt[MN];
ll ilkol[MN];
bool on[MN];
bool onn[MN];
vector<pair<int,ll>>kol[MN];//masa,cena indeks to kolor

int main()
{
ios_base::sync_with_stdio(0); cin.tie(NULL);
int n,k,M,a,b; ll c;
I n>>k>>M;
P(0,n)
{
    I a>>b>>c;
    kol[a].PB({b,c});
}

on[0]=1;
P(0,M)
{ opt[j]=inf; nkoszt[j]=inf;}opt[0]=0;
P(1,M+1)
{
    P3(1,k+1,z)
    {
        for(pair<int,ll>x:kol[z])
        {
        P3(0,M,i)
        {
            if(on[i])
            {
                nkoszt[(i+x.F)%M]=min(koszt[i]+x.S,nkoszt[(i+x.F)%M]);
                onn[(i+x.F)%M]=1;
            }
        }
        }
        P3(0,M,i)
        {
            on[i]=onn[i];onn[i]=0;
            koszt[i]=nkoszt[i];

        }
    }
    P3(0,M,i)
    { if(on[i])opt[i]=min(nkoszt[i],opt[i]); nkoszt[i]=inf;}
}
P(0,M)
{
    if(opt[j]==inf)
        O -1<<E;
    else O opt[j]<<E;
}



return 0;
}