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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
//
// Mateusz Pietrowcow
//
 
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
 
#define MOD 1000000007
#define INF 1000000000
#define INFL 1000000000000000000ULL
#define Tak cout << "TAK" << '\n'
#define Nie cout << "NIE" << '\n'
#define min3(x, y, z) min(x, min(y, z))
#define max3(x, y, z) max(x, max(y, z))
 
using namespace std;
 
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> ii;
typedef pair<long long, long long> pll;
typedef pair<unsigned long long, unsigned long long> pull;

struct Node
{
    int parent;
    bool isMarked;
    list<int> children;
    list<int>::iterator pos;
    pair<ull,int> info;

    Node(pair<ull, int> _info)
    {
        info = _info;
        parent = -1;
        isMarked = false;
    }
};

const int N = 7e3 + 5, MAXDEGREE = 15;

struct fastpq
{
    vector<int> dg[MAXDEGREE];
    list<int> roots;
    Node *t[N];
    int minNode;

    fastpq()
    {
        minNode = N - 1;
        t[N - 1] = new Node({ULLONG_MAX, N - 1});
    }

    // done
    bool empty()
    {
        return roots.empty();
    }
    
    // done
    pair<ull, int> top()
    {
        return t[minNode]->info;
    }
    
    // done
    int Union(int a, int b)
    {
        if (t[a]->info.first <= t[b]->info.first && minNode != b)
        {
            t[b]->parent = a;
            t[a]->children.push_back(b);
            t[b]->pos = prev(t[a]->children.end());
            return a;
        }

        t[a]->parent = b;
        t[b]->children.push_back(a);
        t[a]->pos = prev(t[b]->children.end());
        return b;
    }

    // done
    void clean()
    {
        int newMin = N - 1;

        for (auto i : roots)
        {
            if (i == minNode) continue;

            dg[t[i]->children.size()].push_back(i);

            if (t[i]->info.first < t[newMin]->info.first)
                newMin = i;
        }
        minNode = newMin;

        roots.clear();

        for (int i = 0; i < MAXDEGREE; i++)
        {
            while (!dg[i].empty())
            {
                if (dg[i].size() == 1)
                {
                    roots.push_back(dg[i].back());
                    dg[i].pop_back();
                }
                else
                {
                    int a = dg[i].back(), b;
                    dg[i].pop_back();
                    b = dg[i].back();
                    dg[i].pop_back();

                    dg[i + 1].push_back(Union(a, b));
                }
            }
        }
    }

    // done
    void pop()
    {
        int node = minNode;

        for (auto i : t[node]->children)
        {
            t[i]->parent = -1;
            roots.push_back(i);
        }

        delete t[node];
        t[node] = NULL;

        clean();
    }

    // done
    void cut(int node)
    {
        if (t[node]->parent == -1) return;

        int p = t[node]->parent;

        t[p]->children.erase(t[node]->pos);
        roots.push_back(node);
        t[node]->parent = -1;
        t[node]->isMarked = false;

        if (!t[p]->isMarked) t[p]->isMarked = true;
        else cut(p);
    }

    // done
    void decreaseKey(int node)
    {
        int p = t[node]->parent;

        if (p == -1) return;
        if (t[p]->info.first <= t[node]->info.first) return;

        cut(node);
    }

    // done
    void push(pair<ull, int> val)
    {
        if (t[val.second] == NULL)
        {
            t[val.second] = new Node(val);
            roots.push_back(val.second);
        }
        else
        {
            if (t[val.second]->info.first <= val.first)
                return;
            t[val.second]->info.first = val.first;
            decreaseKey(val.second);
        }

        if (val.first < t[minNode]->info.first)
            minNode = val.second;
    }
};

fastpq pq;
ull dp[N][N], dp2[N];
vector<ii> color[N];
vector<pair<ull, int>> items;

int n, K, M;

void read()
{
    cin >> n >> K >> M;

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

void getMoves()
{
    for (int i = 0; i <= K; i++)
        for (int j = 0; j < M; j++)
            dp[j][i] = INFL;
    dp[0][0] = 0;

    for (int i = 0; i < K; i++)
    {
        for (int j = 0; j < M; j++)
        {
            if (dp[j][i] == INFL) continue;
            
            for (auto&[u, w] : color[i + 1])
                dp[(j + u) % M][i + 1] = 
                    min(dp[(j + u) % M][i + 1], dp[j][i] + (ull)w);
        }
    }

    for (int i = 1; i < M; i++)
        if (dp[i][K] != INFL)
            items.push_back({dp[i][K], i});
}

void dijkstra()
{
    for (auto&i : items)
        pq.push(i);

    while (!pq.empty())
    {
        auto acc = pq.top();
        pq.pop();

        if (dp2[acc.second] != INFL) continue;

        dp2[acc.second] = acc.first;

        for (auto&[cost, m] : items)
        {
            int r = (acc.second + m) % M;

            if (dp2[r] != INFL) continue;

            pq.push({acc.first + cost, r});
        }
    }
}

void solve()
{
    for (int i = 1; i < M; i++) dp2[i] = INFL;

    getMoves();
    dijkstra();

    for (int i = 0; i < M; i++)
    {
        if (dp2[i] == INFL) cout << "-1\n";
        else cout << dp2[i] << '\n';
    }
}
 
int main()
{
    ios_base::sync_with_stdio(0);
    cout.tie(0);
    cin.tie(0);
 
    read();
    solve();
}