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
81
82
83
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using llu = unsigned long long;

#define int ll

const int inf = 1e15;

void optimize(vector<int> &a) {
    int n = a.size();

    int iters = 0;
    while (true) {
        iters++;
        vector<int> new_dp = a;
        for (int i = 0; i < n; i++) {
            if (a[i] >= inf) continue;
            for (int j = i; j < n; j++) {
                int index = (i + j) % n;
                new_dp[index] = min(new_dp[index], a[i] + a[j]);
            }
        }
        if (new_dp == a) break;
        a = new_dp;
        /*
        cout << iters << ": ";
        for (int i = 0; i < n; ++i) {
            cout << (a[i] == inf ? -1 : a[i]) << " ";
        }
        cout << '\n';
        */
    }
    iters--;

    cerr << iters << " iterations\n";
    /*
    for (int i = 0; i < n; ++i) {
        cerr << (a[i] == inf ? -1 : a[i]) << " ";
    }
    cerr << '\n';
    */
}
const int MAX_K = 1e4;
struct Zelek {
    int masa;
    int cena;
};
vector<Zelek> zelki[MAX_K];

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int n, K, m;
    cin >> n >> K >> m;
    for (int i = 0; i < n; ++i) {
        int k, m, c;
        cin >> k >> m >> c;
        k--;
        zelki[k].push_back({m, c});
    }
    vector<int> dp(m, inf);
    dp[0] = 0;
    for (int k = 0; k < K; k++) {
        vector<int> new_dp(m, inf);
        for (int i = 0; i < m; i++) {
            for (auto z : zelki[k]) {
                int index = (i + z.masa) % m;
                new_dp[index] = min(new_dp[index], dp[i] + z.cena);
            }
        }
        dp = new_dp;
    }
    dp[0] = 0;
    optimize(dp);
    for (int i = 0; i < m; ++i) {
        cout << (dp[i] == inf ? -1 : dp[i]) << "\n";
    }
}