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
//GRT_2018
#include <bits/stdc++.h>
#define PB push_back
#define ST first
#define ND second
#define all(x) x.begin(), x.end()
//mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

using namespace std;

using ll = long long;
using pi = pair<int,int>;
using vi = vector<int>;

const int nax = 7000 + 10;
const ll INF = 1e18;
int n, k, m;
vector<pi>items[nax];
ll dp[nax][2];
ll DP[nax][2];

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
    cin >> n >> k >> m;
    for (int col, mass, cost, i = 0; i < n; ++i) {
        cin >> col >> mass >> cost;
        items[col].emplace_back(mass, cost);
    }

    for (int i = 0; i < m; ++i) dp[i][0] = INF;
    dp[0][0] = 0;

    for (int col = 1; col <= k; ++col) {
        for (int i = 0; i < m; ++i) {
            dp[i][col%2] = INF;
        }
        for (auto [mass, cost] : items[col]) {
            for (int i = 0; i < m; ++i) {
                dp[(i + mass) % m][col%2] = min(dp[(i + mass) % m][col%2], dp[i][1-(col%2)] + cost);
            }
        }
    }
    DP[0][0] = 0;
    for (int i = 1; i < m; ++i) DP[i][0] = INF;

    for (int r = 1; r < m; ++r) {
        for (int i = 0; i < m; ++i) DP[i][r%2] = INF;
        
        int g = gcd(r, m);
        for (int r2 = 0; r2 < g; ++r2) {
            ll best = INF;
            ll cost = 0;
            for (int rep = 0; rep < m / g; ++rep) {
                best = min(best, DP[(r2 - (ll)(r-m) * rep) % m][1-(r%2)] + cost);
                cost += dp[r][k % 2];
                if (cost > INF) break;
            }

            for (int rep = 0, i = r2; rep < m / g; ++rep, i = (i + r) % m) {

                best = min(best, DP[i][1 - (r%2)]);
                
                DP[i][r%2] = min(DP[i][r%2], best);

                best += dp[r][k%2];

            }
        }
    }
    
    for (int i = 0; i < m; ++i) {
        cout << (DP[i][(m-1)%2] == INF ? -1 : DP[i][(m-1)%2]) << "\n";
    }
}