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
// PA2024 runda 3B -  https://sio2.mimuw.edu.pl/c/pa-2024-1/p/zel/
// 19:20-20:20, nadal kartka
// 8:30 - 8:40, 10:40-11:40 +  1h, 18-19, 19.15-20.15
//-std=c++20
#include<iostream>
#include <cstddef>
#include <algorithm>
#include <numeric>
#include<vector>
#include<map>
#include<list>

#define MIN(a, b) ((a)<(b)?(a):(b))

using I = int64_t;


struct RemindersCost {
    std::map<I, I> v;

    void rotate(I x, I m) {
        std::map<I, I> z = v;
        v.clear();
        for (const auto &item: z) {
            v[(item.first + x) % m] = item.second;
        }
    }

    void add(I cost) {
        for (const auto &item: v) {
            v[item.first] += cost;
        }
    }

    void put(I reminder, I cost) {
        if (v.contains(reminder)) {
            v[reminder] = MIN(v[reminder], cost);
        } else {
            v[reminder] = cost;
        }
    }

    void put(RemindersCost &other) {
        for (const auto &item: other.v) {
            put(item.first, item.second);
        }
    }

    void merge(RemindersCost &other, I m) {
        std::map<I, I> copy = v;
        v.clear();
        for (const auto &x: copy) {
            for (const auto &y: other.v) {
                auto reminder = (x.first + y.first) % m;
                auto cost = x.second + y.second;
                put(reminder, cost);
            }
        }
    }
};

struct CostMatrix {
    std::vector<RemindersCost> levels;

    void merge(CostMatrix &other) {
        for (int i = 1; i < levels.size(); ++i) {
            levels[i].merge(other.levels[i], levels.size());
        }
    }

};

struct Input {
    I n, k, m; // <1, 7k>
    std::vector<I> colors; // <1, 7k>
    std::vector<I> masses; // <1, 7k>
    std::vector<I> costs; // <1, 7k>
};

std::istream &operator>>(std::istream &is, Input &x) {
    is >> x.n >> x.k >> x.m;
    x.colors.resize(x.n);
    x.masses.resize(x.n);
    x.costs.resize(x.n);
    for (I i = 0; i < x.n; ++i) {
        is >> x.colors[i] >> x.masses[i] >> x.costs[i];
        x.colors[i]--; //index from 0
    }
    return is;
}

struct Multiplar {
    std::vector<I> reminders;
    std::vector<I> costs;
    CostMatrix cost_matrix;

    void calculate_cost_matrix(I m) {
        //std::vector<I> i_reminders(reminders.size());
        cost_matrix.levels.resize(m);
        cost_matrix.levels[0].put(0, 0);
        for (int level = 1; level < m; ++level) {
            //next_reminders(i_reminders, m);
            for (int ith_option = 0; ith_option < reminders.size(); ith_option++) {
                auto copy = cost_matrix.levels[level - 1];
                copy.rotate(reminders[ith_option], m);
                copy.add(costs[ith_option]);
                cost_matrix.levels[level].put(copy);
            }
        }
    }

//private:
//    void next_reminders(std::vector<I> &i_reminders, I m) {
//        for (int i = 0; i < i_reminders.size(); ++i) {
//            i_reminders[i] = (i_reminders[i] + reminders[i]) % m;
//        }
//    }
};


struct Zel {

    I m;
    I singular_cost = 0;
    I singular_reminder = 0;
    std::list<Multiplar> multiplars;
    bool missing_color = false;

    void stats() {
        Input input;
        std::cin >> input;
        m = input.m;
        std::vector<std::vector<I>> jellies_by_color;
        jellies_by_color.resize(input.k, std::vector<I>());
        for (I ith_jelly = 0; ith_jelly < input.n; ++ith_jelly) { //map color -> jelly
            I ith_jelly_color = input.colors[ith_jelly];
            bool found = false;
            for (auto &same_color_jelly: jellies_by_color[ith_jelly_color]) { // check duplicates, if found, take cheaper
                found = input.masses[ith_jelly] == input.masses[same_color_jelly];
                if (found && input.costs[ith_jelly] < input.costs[same_color_jelly]) {
                    same_color_jelly = ith_jelly;
                    break;
                }
            };
            if (!found) {
                jellies_by_color[ith_jelly_color].push_back(ith_jelly);
            }
        }
        for (int i = 0; i < input.k; ++i) {
            if (jellies_by_color[i].empty()) {
                missing_color = true;
            }
        }
        for (int i = 0; i < input.k; ++i) {
            if (jellies_by_color[i].size() == 1) {
                singular_cost += input.costs[jellies_by_color[i][0]];
                singular_reminder = (singular_reminder + input.masses[jellies_by_color[i][0]]) % input.m;
            }
        }

        // singularow nie potrzebujemy już
        // innych szczegolow rownież
        for (int ith_color = 0; ith_color < input.k; ++ith_color) {
            I jelly_count = jellies_by_color[ith_color].size();
            if (jelly_count > 1) {
                multiplars.push_back(Multiplar());
                Multiplar &mp = multiplars.back();
                mp.reminders.resize(jelly_count);
                mp.costs.resize(jelly_count);
                for (I j = 0; j < jelly_count; j++) {
                    mp.costs[j] = input.costs[jellies_by_color[ith_color][j]];
                    mp.reminders[j] = input.masses[jellies_by_color[ith_color][j]];
                }
            }
        }
    }

    void run() {
        stats();
        if (missing_color) {
            std::cout << "0\n";
            for (int i = 1; i < m; ++i) {
                std::cout << "-1\n";
            }
        } else {
            for (auto &item: multiplars) {
                item.calculate_cost_matrix(m);
            }

            CostMatrix x;
            x.levels.resize(m);
            I i_reminder = 0;
            I i_cost = 0;
            x.levels[0].put(0, 0);
            for (int i = 1; i < m; ++i) {
                i_reminder = (i_reminder + singular_reminder) % m;
                i_cost += singular_cost;
                x.levels[i].put(i_reminder, i_cost);
            }

            for (auto &item: multiplars) {
                x.merge(item.cost_matrix);
            }

            for (int i = 0; i < m; ++i) {
                bool found = false;
                I cost = std::numeric_limits<I>::max();
                for (int level = 0; level < m; ++level) {
                    if (x.levels[level].v.contains(i)) {
                        found = true;
                        cost = std::min(cost, x.levels[level].v[i]);
                    }
                }
                if (found) {
                    std::cout << cost << '\n';
                } else {
                    std::cout << "-1\n";
                }
            }
        }

    }


};

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
    Zel zel;
    zel.run();

}