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
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned int ui;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<long long> vll;
const ll LINF = 1e18;
const int INF = 1e9;

ll dp1_global[7000]; // m
ll dp2_global[7000]; // m
vi el_list1_global;
vi el_list2_global;
ll result[7000];

struct Jelly {
    int m;
    int c;
};

int if_mod(int x, int mod) {
    return x >= mod ? x - mod : x;
}

bool any_empty(vector<vector<Jelly>> &v) {
    for (size_t i = 0; i < v.size(); ++i) {
        if (v[i].size() == 0) {
            return true;
        }
    }
    return false;
}

ll *solve(int m, vector<vector<Jelly>> &jellies) {
    int k = jellies.size();

    ll *dp1 = dp1_global;
    ll *dp2 = dp2_global;
    vi *el_list1 = &el_list1_global;
    vi *el_list2 = &el_list2_global;
    el_list1->reserve(m);
    el_list2->reserve(m);

    for (int i = 0; i < m; ++i) {
        dp1[i] = LINF;
        dp2[i] = LINF;
        result[i] = LINF;
    }
    dp1[0] = 0;
    el_list1->push_back(0);
    result[0] = 0;

    for (int i = 0; i < k; ++i) {
        for (size_t j = 0; j < el_list2->size(); ++j) {
            dp2[(*el_list2)[j]] = LINF;
        }
        el_list2->clear();

        for (size_t j = 0; j < el_list1->size(); ++j) {
            int el = (*el_list1)[j];
            for (size_t n_i = 0; n_i < jellies[i].size(); ++n_i) {
                Jelly &jelly = jellies[i][n_i];
                int new_mass = if_mod(el + jelly.m, m);
                ll new_cost = dp1[el] + jelly.c;

                if (dp2[new_mass] == LINF) {
                    dp2[new_mass] = new_cost;
                    el_list2->push_back(new_mass);
                } else {
                    dp2[new_mass] = min(dp2[new_mass], new_cost);
                }
            }
        }

        swap(dp1, dp2);
        swap(el_list1, el_list2);
    }

    dp1[0] = 0;
    for (int m_i = 2; m_i < m; ++m_i) {
        for (int i = 0; i < m; ++i) {
            dp2[i] = LINF;
        }

        bool improved = false;
        for (int i = 0; i < m; ++i) {
            for (int j = 0; j < m; ++j) {
                int new_mass = if_mod(i + j, m);
                ll new_cost = dp1[i] + dp1[j];
                dp2[new_mass] = min(dp2[new_mass], new_cost);
            }
        }

        for (int i = 0; i < m; ++i) {
            if (result[i] > dp2[i]) {
                result[i] = dp2[i];
                improved = true;
            }
        }

        if (!improved) {
            break;
        }
        swap(dp1, dp2);
    }

    return result;
}

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

    int n, k, m;
    cin >> n >> k >> m;
    vector<vector<Jelly>> jellies(k);

    int k_i, m_i, c_i;
    for (int i = 0; i < n; ++i) {
        cin >> k_i >> m_i >> c_i;
        jellies[k_i - 1].push_back({if_mod(m_i, m), c_i});
    }

    if (any_empty(jellies)) {
        cout << "0\n";
        for (int i = 1; i < m; ++i) {
            cout << "-1\n";
        }
        return 0;
    }
    
    ll *res = solve(m, jellies);
    for (int i = 0; i < m; ++i) {
        if (res[i] == LINF) {
            cout << "-1\n";
        } else {
            cout << res[i] << '\n';
        }
    }

    return 0;
}