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

const long long MAKS = 1e18 + 10;
int n,m,k;
vector<pair<long long,long long>> zelek[7020];

long long dp[7020];
bool found[7020];
long long result[7020];
long long upd[7020];

void update_dp(int i){
    long long res[7020];
    for(int i=0;i<m; i++){
        res[i] = MAKS;
    }
    
    for(auto p: zelek[i]){
        int r = p.first % m;
        long long val = p.second;
        
        for(int i=0; i<m; i++){
            res[(r+i)%m] = min(res[(r+i)%m], val+dp[i]);
        }
    }
    
    for(int i=0;i<m; i++){
        dp[i] = res[i];
    }
}

int find_result(){
    long long mini = MAKS;
    for(int i=0;i<m;i++){
        if(found[i] == 0) mini = min(mini, dp[i]);
    }
    if(mini == MAKS) return -1;
    for(int i=0;i<m;i++){
        if(found[i] == 0 && dp[i] == mini) return i;
    }
}

void update_result(int r, long long val){
    for(int i=0;i<m;i++){
        upd[(i+r)%m] = dp[i] + val;
    }
    for(int i=0;i<m;i++){
        dp[i] = min(dp[i], upd[i]);
    }
}

int main(){
    ios_base::sync_with_stdio(0);
    cin >> n >> k >> m;
    for(int i=0;i<n;i++){
        int a,b,c;
        cin >> a >> b >> c;
        zelek[a].push_back({b,c});
    }
    for(int i=1; i<m; i++) dp[i] = MAKS;

    for(int i=1;i<=k;i++){
        update_dp(i);
    }
    
    while(true){
        int ind = find_result();
        if(ind == -1) break;
        found[ind] = 1;
        result[ind] = dp[ind];
        update_result(ind, dp[ind]);
    }
    
    cout<<"0"<<endl;
    for(int i=1;i<m;i++){
        if(found[i] == 0) cout<<"-1"<<endl;
        else cout<<result[i]<<endl;
    }
}