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

constexpr long long INF = 1e18;
long long n,k,m;

struct zelek {
    long long masa;
    long long cena;
};

vector<vector<zelek>> kolory;
vector<long long> single;

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

    cin >> n >> k >> m;

    kolory = vector<vector<zelek>>(k);
    single = vector<long long>(m, INF);
    vector<vector<long long>> temp_pack(2, vector<long long>(m, INF));
    temp_pack[0][0] = 0;

    for (int i = 0, ki, mi, ci; i < n; i++) {
        cin >> ki >> mi >> ci;
        kolory[ki - 1].push_back((zelek){
            .masa = mi,
            .cena = (long long)ci
        });
    }

    for (int i = 0; i < k; i ++) {
        for (auto e : kolory[i]) {
            for (int j = 0; j < m; j++) {
                temp_pack[(i & 1) ^ 1][(j + e.masa) % m] = min(
                        temp_pack[(i & 1) ^ 1][(j + e.masa) % m], 
                        temp_pack[i & 1][j] + e.cena
                    );
            }
        }
        std::fill(temp_pack[i & 1].begin(), temp_pack[i & 1].end(), INF);
    }
    std::swap(single, temp_pack[k & 1]);
    single[0] = 0;

    vector<long long> multiple(m, INF);

    for (long long i = 0; i < m; i++) {
        if (single[i] >= INF)
            continue;
        for (long long j = 0; j < m; j++) {
            multiple[(i * j) % m] = min(
                    multiple[(i * j) % m],
                    (long long)j * single[i]
                );
        }
    }

    vector<long long> ans(m, INF);

    ans[0] = 0;
    for (long long i = 0; i < m; i++) {
        if (multiple[i] >= INF)
            continue;
        for (long long j = 0; j < m; j++) {
            ans[(i + j) % m] = min(
                    ans[(i + j) % m],
                    ans[j] + multiple[i]
                );
        }
    }

    for (long long v : ans)
        cout << (v == INF ? -1 : v) << "\n";
}