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
// Kacper Orszulak
#ifndef DEBUG
    #define NDEBUG
#endif

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;
using vb = vector<bool>;
using vvi = vector<vi>;
using vvll = vector<vll>;
#define st first
#define nd second
#define pb push_back
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define whole(x) x.begin(), x.end()
[[maybe_unused]] constexpr ll INF = (ll)1e18+7ll;
template<class T> bool minR(T &a, const T &b) {
	if (b < a) { a = b; return true; }
	else return false;
}
template<class T> bool maxR(T &a, const T &b) {
	if (b > a) { a = b; return true; }
	else return false;
}

int n, colorsCnt, MOD;
vector<vector<pii>> jelliesByColor; // mass, cost of single
vll minCostOfSingleColorByMass;
vll minCostOfMultipliedColorByMass;
vll minCostByMass;

void calculateSingleColors() {
    vll minCostOfSingleColorByMassExt[2];

    minCostOfSingleColorByMassExt[0].resize(MOD, INF);
    minCostOfSingleColorByMassExt[1].resize(MOD, INF);
    minCostOfSingleColorByMassExt[1][0] = 0;

    for (int color = 0; color < colorsCnt; ++color) {
        minCostOfSingleColorByMassExt[color%2].assign(MOD, INF);

        for (const auto &[mass, cost] : jelliesByColor[color]) {
            for (int prevMass = 0; prevMass < MOD; ++prevMass) {
                minR(
                    minCostOfSingleColorByMassExt[color%2][(prevMass+mass)%MOD],
                    minCostOfSingleColorByMassExt[(color+1)%2][prevMass] + (ll)cost
                    );
            }
        }
    }

    minCostOfSingleColorByMass = minCostOfSingleColorByMassExt[(colorsCnt-1)%2];
}

void calculateMultipliedColors() {
    minCostOfMultipliedColorByMass.resize(MOD, INF);
    minCostOfMultipliedColorByMass[0] = 0;

    for (int mass = 0; mass < MOD; ++mass) {
        for (int mult = 0; mult < MOD; ++mult) {
            if (minCostOfSingleColorByMass[mass] < INF) {
                minR(
                    minCostOfMultipliedColorByMass[(mass*mult)%MOD],
                    minCostOfSingleColorByMass[mass] * ll(mult)
                    );
            }
        }
    }
}

void calculateRest() {
    vll minCostByMassExt[2];
    minCostByMassExt[0].resize(MOD, INF);
    minCostByMassExt[1].resize(MOD, INF);
    minCostByMassExt[1][0] = 0;

    // minCostByMass.resize(MOD, INF);

    for (int addMass = 0; addMass < MOD; ++addMass) {
        minCostByMassExt[addMass%2].assign(MOD, INF);

        for (int prevMass = 0; prevMass < MOD; ++prevMass) {
            minR(
                minCostByMassExt[addMass%2][(prevMass + addMass)%MOD],
                minCostByMassExt[(addMass+1)%2][prevMass] + minCostOfMultipliedColorByMass[addMass]
                );

            // minR(
            //     minCostByMass[(prevMass + addMass)%MOD],
            //     minCostByMassExt[addMass%2][(prevMass + addMass)%MOD]
            //     );
        }

        for (int allMass = 0; allMass < MOD; ++allMass) {
            minR(
                minCostByMassExt[addMass%2][allMass],
                minCostByMassExt[(addMass+1)%2][allMass]
                );
        }
    }

    minCostByMass = minCostByMassExt[(MOD-1)%2];
}

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

    cin >> n >> colorsCnt >> MOD;
    jelliesByColor.resize(colorsCnt);
    rep(i, n) {
        int color, mass, cost;
        cin >> color >> mass >> cost;
        --color;
        mass %= MOD;
        jelliesByColor[color].push_back({mass, cost});
    }

    calculateSingleColors();
    calculateMultipliedColors();
    calculateRest();

    for (int mass = 0; mass < MOD; ++mass) {
        if (minCostByMass[mass] != INF)
            cout << minCostByMass[mass] << '\n';
        else
            cout << "-1\n";
    }
}