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
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>

#define LL long long
#define PIL pair<int, LL>

using namespace std;

constexpr LL INF = 1e18;

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

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

    vector<vector<PIL>> items(k + 1);
    vector<vector<LL>> DP(2, vector<LL>(m, INF));

    int a, b;
    LL c;
    while (n--)
    {
        cin >> a >> b >> c;
        items[a].emplace_back(b, c);
    }

    DP[0][0] = 0ll;
    for (int r = 1; r <= k; ++r)
    {
        for (auto &[w, c] : items[r])
        {
            for (int i = 0; i < m; ++i)
            {
                DP[r & 1][i] = min(DP[r & 1][i], c + DP[(r + 1) & 1][(m + i - w) % m]);
            }
        }

        DP[(r + 1) & 1].assign(m, INF);
    }

    vector<LL> DP2(m + 1, INF);
    vector<bool> processed(m);
    DP2[0] = 0ll;

    for (int i = 1; i < m; ++i)
    {
        int v = m;
        for (int j = 0; j < m; ++j)
        {
            if (!processed[j] && DP2[j] < DP2[v]) v = j;
        }

        processed[v] = true;
        for (int j = 1; j < m; ++j)
        {
            DP2[(v + j) % m] = min(DP2[(v + j) % m], DP2[v] + DP[k & 1][j]);
        }
    }

    for (int i = 0; i < m; ++i)
    {
        if (DP2[i] == INF) cout << "-1\n";
        else cout << DP2[i] << "\n";
    }
}