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
#include <bits/stdc++.h>
using namespace std;
#define st first
#define nd second
#define ll long long


const ll maxn = 15e3 + 7, INF = LLONG_MAX / 2;
ll n, k, m, x, a, b, licz;
vector<pair<ll, ll>> p[maxn];
ll nef[maxn], res[maxn], dp[maxn];

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> k >> m;
    for(int i = 1; i <= n; i++){
        cin >> x >> a >> b;
        p[x].push_back({a, b});
    }
    for(int i = 0; i <= m; i++)
        res[i] = INF;
    for(int i = 0; i < p[1].size(); i++){
        res[(p[1][i].st)%m] = min(res[(p[1][i].st) % m], p[1][i].nd);
    }
    for(int i = 2; i <= k; i++){
        for(int j = 0; j <= m; j++)
            nef[j] = INF;
        for(int j = 0; j < p[i].size(); j++){
            for(int l = 0; l <= m - p[i][j].st - 1; l++)
                nef[p[i][j].st + l] = min(nef[p[i][j].st + l], res[l] + p[i][j].nd);

            for(int l = m - p[i][j].st; l < m; l++){
                nef[p[i][j].st + l - m] = min(nef[p[i][j].st + l - m], res[l] + p[i][j].nd);
            }
        }
        for(int i = 0; i < m; i++){
            res[i] = nef[i];
        }
        
    }
    res[0] = 0;
    //  for(int i = 0; i < m; i++)
    //     cout << res[i] << " ";
    // cout << '\n';
    //////////// dp
    for(int i = 1; i <= m; i++)
        dp[i] = INF;
    dp[0] = 0;
    for(int i = 1; i < m; i++){
        for(int j = 0; j < m; j++){
            licz = 1;
            while(dp[(j + (i * licz)) % m] > dp[j] + res[i] * licz){
                dp[(j + (i * licz)) % m] = min(dp[(j + (i * licz)) % m], dp[j] + res[i] * licz);
                licz++;
            }
        }
    }
    for(int i = 0; i < m; i++){
        if(dp[i] == INF)
            cout << -1 << '\n';
        else
            cout << dp[i] << '\n';
    }
}