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

const int MAXK = 7000+5;
const int MAXM = 7000+5;
const int MAXN = 7000+5;
const int64_t INF = 1000000000000000000ll;

int n,k,m;

struct Candy {
    int m;
    int64_t c;
};

Candy C[MAXK][MAXN];
int S[MAXK];
int64_t D[2][MAXM];
int64_t P[MAXM];

int main() {
    std::ios_base::sync_with_stdio(0);
    std::cin >> n >> k >> m;
    for (int i=0;i<n;++i) {
        int mc, kc;
        int64_t cc;
        std::cin >> kc >> mc >> cc;
        C[kc][S[kc]] = {.m=mc, .c=cc};
        ++S[kc];
    }
    int l=0;
    int r=1;
    for (int i=0;i<m;++i) D[l][i] = D[r][i] = INF;
    D[r][0] = 0;
    for (int i=1;i<=k;++i) {
        for (int o=0;o<m;++o) {
            D[l][o] = INF;
            for (int j=0;j<S[i];++j) {
                int mm = (o+m-C[i][j].m) % m;
                D[l][o] = std::min(D[l][o], D[r][mm] + C[i][j].c);
            }
        }
        std::swap(l,r);
    }

    for (int i=0;i<m;++i) P[i] = D[r][i];
    P[0] = 0;
    for (int j=1;j<m;j++) {
        bool update = false;
        for (int i=0;i<m;++i) {
            for (int o=0;o<m;++o) {
                int mm = (i+(m-o)) % m;
                int64_t pp = P[mm] + D[r][o];
                if (pp < P[i]) {
                    P[i] = pp;
                    update = true;
                }
            }
        }
        if (!update) break;
    }

    for (int i=0;i<m;++i) std::cout << (P[i] >= INF ? -1 : P[i]) << "\n";

    return 0;
}