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
84
85
86
87
88
89
90
91
92
93
94
#include <bits/stdc++.h>

using namespace std;


struct Candy {
    int k;
    int m;
    long long c;
};


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

    int n, k, m;
    map<int, vector<Candy>> candies;

    cin >> n >> k >> m;

    for (int i = 0; i < n; i++) {
        int k, m;
        long long c;
        cin >> k >> m >> c;
        candies[k].push_back({k, m, c});
    }

    vector<vector<long long>> dp(k + 1, vector<long long>(m, -1));

    for (const auto& candy : candies[1]) {
        if (dp[1][candy.m%m] == -1) {
            dp[1][candy.m%m] = candy.c;
        } else {
            dp[1][candy.m%m] = min(dp[1][candy.m%m], candy.c);
        }
    }

    for (int color = 2; color <= k; color++) {
        for (int r = 0; r < m; r++) {
            for (const auto& candy : candies[color]) {

                if (dp[color - 1][r] != -1) {
                    if (dp[color][(r + candy.m)%m] == -1) {
                        dp[color][(r + candy.m)%m] = dp[color - 1][r] + candy.c;
                    } else {
                        dp[color][(r + candy.m)%m] = min(dp[color][(r + candy.m)%m], dp[color - 1][r] + candy.c);
                    }
                }
            }
        }
    }

    vector <bool> used(m + 1, false);

    for (int i = 1; i < m; i++) {
        long long min_value = LLONG_MAX;
        int min_value_id = -1;

        for (int j = 1; j < m; j++) {
            if (dp[k][j] != -1 && !used[j]) {
                if (dp[k][j] < min_value) {
                    min_value = dp[k][j];
                    min_value_id = j;
                }
            }
        }
        if (min_value_id == -1) {
            break;
        }

        used[min_value_id] = true;

        for (int j = 1; j < m; j++) {
            if (dp[k][j] != -1) {
                if (dp[k][(j + min_value_id)%m] == -1) {
                    dp[k][(j + min_value_id)%m] = dp[k][j] + min_value;
                } else {
                    if (dp[k][j] + min_value < dp[k][(j + min_value_id)%m]) {
                        dp[k][(j + min_value_id)%m] = dp[k][j] + min_value;
                    }
                }
            }
        }
    }


    cout << 0 << endl;
    for (int i = 1; i < m; i++) {
        cout << dp[k][i] << '\n';
    }

    return 0;
}