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

constexpr int N = 7e3;
vector<pair<int,ll>> zelki[N+9];
ll dp1[2][N+9];
ll dp2[N+9];

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n,k,m,a,b,c;
    cin >> n >> k >> m;
    for (int x=0;x<n;x++){
        cin >> a >> b >> c;
        zelki[a].push_back({b,c});
    }
    for (int x=0;x<=m;x++)dp1[0][x]=1e18;
    dp1[0][0]=0;
    for (int ko=1;ko<=k;ko++){
        for (int x=0;x<=m;x++)dp1[ko%2][x]=1e18;
        for (int x=0;x<m;x++){
            for (pair<int,ll> ter:zelki[ko]){
                dp1[ko%2][(x+ter.first)%m] = min(dp1[ko%2][(x+ter.first)%m],dp1[(ko+1)%2][x]+ter.second);
            }
        }
    }
    for (int x=0;x<m;x++){
        dp2[x]=dp1[k%2][x];
    }
    dp2[0]=0;
    for (int t=1;t<=14;t++){
        for (int x=0;x<m;x++){
            for (int y=x;y<m;y++){
                dp2[(x+y)%m]=min(dp2[(x+y)%m],dp2[x]+dp2[y]);
            }
        }
    }
    for (int x=0;x<m;x++){
        if (dp2[x]==1e18)dp2[x]=-1;
        cout << dp2[x] << '\n';
    }
    return 0;
}