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

using ll = long long;
const ll INF = 1e18 + 7;;

int n, k, m;

vector <ll> merge(vector <ll> &a, vector <ll> &b) {
    vector <ll> ans(m, INF);
    vector <pair <int, ll>> tmp1, tmp2;
    for (int i = 0; i < m; i++) {
        if (a[i] != INF) tmp1.push_back({i, a[i]});
        if (b[i] != INF) tmp2.push_back({i, b[i]});
    }

    for (auto &[x, y] : tmp1) {
        for (auto &[c, d] : tmp2) {
            int r = (x + c) % m;
            ans[r] = min(ans[r], y + d);
        }
    }
    return ans;
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    cin >> n >> k >> m;
    vector <vector <ll>> v(k, vector <ll>(m, INF));
    vector <int> zlicz(k);
    for (int i = 0; i < n; i++) {
        ll a, b, c;
        cin >> a >> b >> c;
        a--;
        if (b == m) b = 0;
        v[a][b] = min(v[a][b], c);
        zlicz[a]++;
    }

    for (int i = 0; i < k; i++) {
        if (zlicz[i] == 0) {
            cout << 0 << '\n';
            for (int j = 1; j < m; j++) {
                cout << -1 << '\n';
            }
            return 0;
        }
    }

    for (int i = k - 1; i >= 1; i--) {
        v[i - 1] = merge(v[i], v[i - 1]);
    }

    vector <ll> ans(m, INF);
    for (int i = 0; i < m; i++) {
        ans[i] = v[0][i];
    }
    ans[0] = 0;
    
    int cnt = 1;
    while (cnt) {
        vector <ll> nxt = merge(ans, ans);
        cnt = 0;
        for (int i = 0; i < m; i++) {
            if (nxt[i] < ans[i]) {
                ans[i] = nxt[i];
                cnt++;
            }
        }
    }

    for (int i = 0; i < m; i++) {
        cout << (ans[i] != INF ? ans[i] : -1) << '\n';
    }
}