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
#include <bits/stdc++.h>

using namespace std;

template<typename T>
using pair2 = pair<T, T>;

#define int long long
#define pii pair<int,int>
#define pb push_back
#define pf push_front
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define endl "\n"
#define in(x) cin >> x
#define ini(x) int x; in(x)
#define instr(x) string x; in(x)

#define inf 1e18

//https://codeforces.com/blog/entry/62393
struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    ini(n);
    ini(k);
    ini(m);

    // po kolorze: {masa,cena}
    vector<vector<pii>> by_color(k);
    for (int i = 0; i < n; i++) {
        ini(ki);
        ini(mi);
        ini(wi);

        ki--;

        by_color[ki].pb({mi,wi});
    }

    int max_quantity = m;

    // implementacja rozwiązania programowaniem dynamicznym
    vector<vector<vector<int>>> items(k,vector<vector<int>>(m,vector<int>(max_quantity+1,inf)));
    for (int current_color = 0; current_color < k; current_color++) {
        // przypadek bazowy
        items[current_color][0][0] = 0;

        for (int i = 0; i < max_quantity; i++) {
            for (int j = 0; j < m; j++) {
                for (int el = 0; el < by_color[current_color].size(); el++) {
                    if (items[current_color][j][i] != inf) {
                        items[current_color][(j+by_color[current_color][el].first) % m][i+1] = min(items[current_color][(j+by_color[current_color][el].first) % m][i+1],
                                items[current_color][j][i] + by_color[current_color][el].second);
                    }
                }
            }
        }
    }

    // po znalezieniu tablicy items, wykonujemy kolejne programowanie dynamiczne
    vector<int> ans(m,inf);
    for (int jelly_quantity = 0; jelly_quantity <= max_quantity; jelly_quantity++) {
        vector<vector<int>> dp2(k+1,vector<int>(m,inf));

        dp2[0][0] = 0;

        for (int i = 0; i < k; i++) {
            for (int j = 0; j < m; j++) {
                if (dp2[i][j] != inf) {
                    for (int a = 0; a < m; a++) {
                        dp2[i+1][(j + a) % m] = min(dp2[i+1][(j + a) % m], dp2[i][j] + items[i][a][jelly_quantity]);
                    }
                }
            }
        }

        for (int i = 0; i < m; i++) {
            ans[i] = min(ans[i],dp2[k][i]);
        }
    }

    for (int i : ans) {
        if (i == inf) cout << -1 << endl;
        else cout << i << endl;
    }
}