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
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define ll long long
#define ld long double
#define pb push_back
#define nd second
#define st first
#define sz size
#define forr(i, n) for(int i=1;i<=n;i++)
const ll infl=1e18+90;
const int inf=1e9+90;
const int roz=7003;

vector<pair<int,int>> wek[roz];
priority_queue<pair<int,int> > kol;

int tab[roz][roz], zap[roz];

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n, k, m;
    cin>>n>>k>>m;
    for(int i=1;i<=n;i++)
    {
        int a, b, c;
        cin>>a>>b>>c;
        wek[a].pb({b, c});
    }
    for(int i=0;i<=k;i++)
    {
        for(int j=0;j<m;j++)
        {
            tab[i][j]=infl;
        }
    }
    tab[0][0]=0;
    for(int i=1;i<=k;i++)
    {
        for(auto [r, c]:wek[i])
        {
            for(int j=0;j<m;j++)
            {
                int nowy=(j+r)%m;
                tab[i][nowy]=min(tab[i][nowy], tab[i-1][j]+c);
            }
        }
    }

    for(int i=0;i<m;i++)
    {
        if(tab[k][i]<infl)
        {
            kol.push({-tab[k][i], i});
        }
    }
    while(!kol.empty())
    {

        pair<int,int> pier=kol.top();
        kol.pop();
        int a=pier.st, b=pier.nd;
        a=-a;
        //cerr<<"lol "<<a<<" "<<b<<"\n";
        if(zap[b]==1) continue;
        tab[k][b]=a;
        zap[b]=1;
        for(int i=0;i<m;i++)
        {
            if(tab[k][i]>=infl) continue;
            int r=b+i, w=tab[k][i]+a;
            r%=m;
            if(zap[r]==0&&tab[k][r]>w)
            {
                kol.push({-w, r});
                tab[k][r]=w;
            }
        }
    }
    cout<<"0\n";
    for(int i=1;i<m;i++)
    {
        if(tab[k][i]>=infl)
        {
            cout<<"-1\n";
        }
        else
        {
            cout<<tab[k][i]<<"\n";
        }
    }
}