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

using namespace std;

int main() {
    std::ios::sync_with_stdio(false);
    cin.tie(NULL);

    int n, k, m;
    cin >> n >> k >> m;

    vector<list<pair<long long, long long>>> zel_per_color(k);
    for (int i = 0; i < n; ++i) {
        int ki;
        long long mi, ci;
        cin >> ki >> mi >> ci;
        zel_per_color[ki - 1].push_back({mi, ci});
    }

    vector<vector<long long>> dp(k, vector<long long>(m, -1));
    dp[k - 1][0] = 0;

    bool there_is_change = true;
    while (there_is_change) {
        there_is_change = false;
        for (int j = 0; j < k; ++j) {
            for (long long r = 0; r < m; ++r) {
                for (auto [mi, ci] : zel_per_color[j]) {
                    long long x = dp[(k + j - 1) % k][(m + r - mi) % m];
                    if (x == -1) {
                        continue;
                    } else if (dp[j][r] < 0) {
                        dp[j][r] = x + ci;
                        there_is_change = true;
                    } else if (x + ci < dp[j][r]) {
                        dp[j][r] = x + ci;
                        there_is_change = true;
                    }
                }
            }
        }
    }

    for (auto x : dp[k - 1]) {
        cout << x << "\n";
    }

    return 0;
}