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
#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif
//#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;

#define pb push_back

typedef long long ll;
typedef long double ld;
int n, k, m;
const int maxN = 7005;
const ll INF = 1e18;
ll dp[maxN];
ll ndp[maxN];
vector<pair<int,int>> clr[maxN];
bool used[maxN];
ll S[maxN];
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
#ifdef DEBUG
    freopen("input.txt", "r", stdin);
#endif
    cin >> n >> k >> m;
    for (int i = 0; i < n; i++) {
        int K, M, C;
        cin >> K >> M >> C;
        M %= m;
        K--;
        clr[K].emplace_back(M, C);
    }
    dp[0] = 0;
    for (int z = 1; z < m; z++) dp[z] = INF;
    for (int i = 0; i < k; i++) {
        for (int z = 0; z < m; z++) ndp[z] = INF;
        for (auto& it : clr[i]) {
            for (int z = 0; z < m; z++) {
                int tz = z + it.first;
                if (tz >= m) tz -= m;
                ndp[tz] = min(ndp[tz], dp[z] + it.second);
            }
        }
        for (int z = 0; z < m; z++) {
            dp[z] = ndp[z];
        }
    }
    S[0] = 0;
    for (int z = 1; z < m; z++) {
        S[z] = INF;
    }
    for (int z = 0; z < m; z++) {
        int who = -1;
        for (int pz = 0; pz < m; pz++) {
            if (!used[pz] && (who == -1 || S[pz] < S[who])) {
                who = pz;
            }
        }
        assert(who != -1);
        used[who] = true;
        for (int pz = 0; pz < m; pz++) {
            int tz = who + pz;
            if (tz >= m) tz -= m;
            S[tz] = min(S[tz], S[who] + dp[pz]);
        }
    }
    //7000 * 7000 * 10^9
    for (int z = 0; z < m; z++) {
        if (S[z] > (ll)5e17) S[z] = -1;
        cout << S[z] << '\n';
    }
    return 0;
}